English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

4 answers

There is no difference in speed that you could experience, but there actually could be.

C compilers have optimizators that in some cases use CPU assembly commands that are faster to execute.

If you select that speed is to be optimized (not size), the
for (i = 10; i >=0; i–)
could be translated in assembly code to something like
i = 10
Loop:
(code)
i--
jump if not zero to Loop

while for(i = 0; i <10; i++) is
i = 0
Loop:
(code)
i++
compare i with 10
jump if less to Loop

You see that there is an additional (compare i<10) assembly command needed and it makes the difference in speed.

2007-01-18 22:23:07 · answer #1 · answered by BataV 3 · 0 0

actually your wrong because the first one goes through 11 items and the second only goes through ten.. 10 - 0 and then 0 - 9 ( you wrote < 10 not <=).

2007-01-19 00:46:21 · answer #2 · answered by Wesley C 3 · 0 0

It shouldn't be... your first statement [ for(i=10;i>=0;i--) ] runs 11 times, while your second statement runs 10 times...

2007-01-18 20:36:15 · answer #3 · answered by Jack 3 · 1 0

Just did some tests... it isn't.

2007-01-18 21:49:35 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers