优化 [英] Optimization

查看:83
本文介绍了优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何替代方法可以优化以下代码

(for loop)

main()

{

int i,j;

for(i = 0; i< 40000000; i ++)

j = 1;

}


只想知道如何优化for循环;我知道代码没有

逻辑...........

但我听说有一种方法可以通过重组aboce来优化/>
代码。 j = 1的赋值必须保留在循环中。

is there any ulternative metho to optimize the following piece of code
(for loop)
main()
{
int i,j;
for(i=0;i<40000000;i++)
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.

推荐答案

Spidey写道:
是否存在任何替代方法来优化下面的代码
(for loop)

main()


int main(void),please。

{
int i,j;
for(i = 0; i< 40000000; i ++)


40000000远远超出标准为int提供的范围。

让它变长。

j = 1;
}

只是想知道如何优化for循环;我知道代码没有逻辑...........
但我听说有一种方法可以通过重构代码进行优化。 j = 1分配必须保持在循环内。
is there any ulternative metho to optimize the following piece of code
(for loop)
main()
int main(void), please.
{
int i,j;
for(i=0;i<40000000;i++)
40000000 is well out of the range provided by the standard for an int.
Make it a long.
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.




你的帖子毫无意义。你所显示的循环相当于完全没有任何内容,并且它包含的唯一语句不会被移动到循环之外的
。没有什么可以优化的。一个好的编译器将通过为i = 40000000;生成代码来优化这一点。 j = 1",并且'

假设变量也没有被优化。


你不能重组"什么都没有。您可能正在考虑可能由编译器在

命令中生成的机器语言指令,以实现此循环。然而,这是另一个新闻组

的问题,它讨论了你的编译器/平台。此外,如果你使用

不同的循环结构实现相同的循环,那么没有编译器值得

它的盐将生成更快的代码,是一段时间,一段时间还是一个转到。


S.



Your post makes no sense. The loop you''ve shown is equivalent to doing
nothing at all, and the only statement it contains is not to be moved
outside of the loop. There''s nothing to optimize. A good compiler will
optimize this by generating code for "i = 40000000; j = 1", and that''s
assuming the variables aren''t optimized out of existence too.

You can''t "restructure" what isn''t there. You may be thinking of the
machine language instructions that might be generated by a compiler in
order to implement this loop. That''s a question for another newsgroup
that discusses your compiler/platform, however. Also, no compiler worth
its salt will generate faster code if you implement the same loop with a
different loop construct, be it a while, a do-while or a goto.

S.


2005年11月25日21:17:59 -0800,蜘蛛侠 <是******** @ gmail.com>写道:
On 25 Nov 2005 21:17:59 -0800, "Spidey" <am********@gmail.com> wrote:
是否有任何替代方法来优化下面的代码
(for loop)

main()
{
int i,j;
for(i = 0; i< 40000000; i ++)
j = 1;
}

只是想知道如何优化for循环;我知道代码没有逻辑...........
但我听说有一种方法可以通过重构代码进行优化。 j = 1的赋值必须保持在循环内。
is there any ulternative metho to optimize the following piece of code
(for loop)
main()
{
int i,j;
for(i=0;i<40000000;i++)
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.




这就是问题所在。一旦你得到关于逻辑的一些线索,那么你就可以看看发生了什么,看看它是否正在做什么

那不是'b $ b'必要的,如果你已经知道某些计算的答案,你是否可以沿着

做出一些假设。在这个

的情况下,由于for循环没有做任何事情,你可以删除for

循环并跳到最后:


int main(无效)

{

long int i = 39999999;

int j = 1;

返回0;

}


仍然无效,但它会更快一点。


现在,你只是将j = 1设置为大约40,000,000次,这是一种坚果。

------ ---------------------------------------

谢谢。

MCheu



That''s kind of the problem. Once you get some clue about the logic,
then you can look at what''s going on and see if it''s doing anything
that''s not necessary and whether you can make some assumptions along
the way if you already know the answer to some calculations. In this
case, since the for loop doesn''t do anything, you can cut out the for
loop and skip to the end:

int main (void)
{
long int i=39999999;
int j=1;
return 0;
}

Still does nothing, but it''ll do it a bit faster.

Right now, you''re just setting j=1 about 40,000,000 times, which is
kind of nuts.
---------------------------------------------
Thanks.
MCheu


MCheu写道:
2005年11月25日21:17:59 -0800,蜘蛛侠 <是******** @ gmail.com>写道:

On 25 Nov 2005 21:17:59 -0800, "Spidey" <am********@gmail.com> wrote:

是否有任何替代方法来优化下面的代码
(for loop)

main()<对于(i = 0; i <40000000; i ++)
j = 1;
}
只是想知道如何优化for循环;我知道代码没有逻辑...........
但我听说有一种方法可以通过重构代码进行优化。 j = 1分配必须保持在循环内。

这就是问题所在。一旦你得到关于逻辑的一些线索,那么你就可以看看发生了什么,看看它是否正在做什么,这是不必要的以及你是否可以做出一些假设如果您已经知道某些计算的答案,那么就这样。在这种情况下,因为for循环没有做任何事情,你可以删除for循环并跳到最后:

int main(void)
{
long int i = 39999999;
is there any ulternative metho to optimize the following piece of code
(for loop)
main()
{
int i,j;
for(i=0;i<40000000;i++)
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.

That''s kind of the problem. Once you get some clue about the logic,
then you can look at what''s going on and see if it''s doing anything
that''s not necessary and whether you can make some assumptions along
the way if you already know the answer to some calculations. In this
case, since the for loop doesn''t do anything, you can cut out the for
loop and skip to the end:

int main (void)
{
long int i=39999999;



ITYM 40000000 int j = 1;
返回0;
}
<仍然什么都不做,但它会更快一点。

现在,你只是设置j = 1约40,000,000次,这是一种坚果。
-------------------------------------------- -
谢谢。

MCheu


ITYM 40000000 int j=1;
return 0;
}

Still does nothing, but it''ll do it a bit faster.

Right now, you''re just setting j=1 about 40,000,000 times, which is
kind of nuts.
---------------------------------------------
Thanks.
MCheu



-

电子邮件:我的是/ at / gmx /点/地址。


--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆