Which of these codes will work faster

I recently started thinking about optimization, now I know that there are many books and articles, but I have a specific scenario that interests me.

and.

for (i = 0; i < limit + 5; i++)
  cout << test;

AT.

limit2 = limit +5;
for (i = 0; i < limit2; i++)
  cout << test;

I want to know if the second piece of code will work faster, because it should only perform mathematical calculation once or is this calculation saved for the life time of the loop.

+5
source share
9 answers

, , , . . , , , . 10% 90% . 90% , , 10% , , .

+3

, , int .., , - . , operator++.

+11

.

, cout << test 99% .

, , . !

(tm)!

+5

. .

+5

B . . A B.

+4

, , / ( ) limit + 5 (A), limit2 (B) for-loop ( , ).

, , A, B / . nitpicking ( Zuse Z1)

+3

:

i limit, ( ).

  • ( int, short, long, char ..), , . , cout. A (, , B), .

  • , i operator< operator++ limit operator+, , ( , ) . , , scenario B , limit2 = limit +5; .

: limit2 = limit +5 , :

 limit2 = limit.operator+(5);

B , for.

+3

- , cout, . , .

+2

, .

, , "limit2" , , "". , , , < , , , . +5 , , .

0

All Articles