循环优化 [英] Loop optimization
问题描述
我有两个版本的循环。我想知道哪一个更有效率b / b
。并且有什么办法可以让它更有效率吗?
1- for(i = 0; str [i ++]!=''\ 0'';);
2- for(i = 0; str []!=''\'''; ++ i);
第二个循环语句应该读作:
for(i = 0; str [i]!='''\\'0' '; ++ i);
抱歉给您带来不便。
Haroon
"哈龙" <公顷*********** @ gmail.com>在消息中写道
news:11 ********************** @ z14g2000cwz.googlegr oups.com ...我有两个版本的循环。我想知道哪一个更有效率。有没有办法让它更有效率?
1- for(i = 0; str [i ++]!=''\ 0'';);
2- for(i = 0; str []!=''\'''; ++ i);
这取决于编译器等。如果为两者生成完全相同的代码(即两者同样有效地为
效率),我不会感到惊讶
。
Alex
haroon写道:第二个循环语句应该读作:
for(i = 0; str [i]!=''\'''; ++ i);
抱歉给您带来不便。
Haroon
C语言标准对效率一无所知,所以
在这里说的不多。你可以在你的机器上测试它们,并且b $ b看哪个更快。这两个循环以不同的价值结束i,所以也许你想做什么?将是
一个更好的起点。也就是说,标准的strlen
可能会更快,因为它可能会使用特定于平台的技巧
(用于确定字符串长度的花哨的汇编指令?)
不适用于标准C.
-David
Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?
1- for (i = 0; str[i++] != ''\0'';) ;
2- for (i = 0; str[] != ''\0''; ++i) ;
the second loop statement should be read as:
for (i = 0; str[i] != ''\0''; ++i) ;
sorry for inconvenience.
Haroon
"haroon" <ha***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?
1- for (i = 0; str[i++] != ''\0'';) ;
2- for (i = 0; str[] != ''\0''; ++i) ;
It depends on the compiler, amongst other things. It would not surprise me
if exactly the same code was generated for both (ie both were equally
efficient).
Alex
haroon wrote:the second loop statement should be read as:
for (i = 0; str[i] != ''\0''; ++i) ;
sorry for inconvenience.
Haroon
C language standard says nothing about efficiency, so
not a lot to say here. You can test them on your machine and
see which is faster. The two loops end up with i at different
values, so perhaps "which does what you want" would be
a better place to start. That said, the standard strlen
MIGHT be faster, as it may use platform specific tricks
(fancy assembly instructions for determining string length?)
not available in standard C.
-David
这篇关于循环优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!