哪个循环更快 [英] which loop is faster

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

问题描述

大家好,


假设我有2个循环,一个在另一个里面,就像这个


1)for(i = 0; i< = 100; i ++)

{

for(j = 0; j< = 10; j ++)

{

一些代码;

}

}


2)for(i = 0; i< = 10; i ++)

{

for(j = 0; j< = 100; j ++)

{

some代码;

}

}


所以循环在第一个或第二个循环中运行得更快。


thnx提前

sushant

hi all,

suppose i have 2 loops one inside the other, like this

1) for(i=0;i<=100;i++)
{
for(j=0;j<=10;j++)
{
some code;
}
}

2) for(i=0;i<=10;i++)
{
for(j=0;j<=100;j++)
{
some code;
}
}

so which loops will work faster the 1st one or the 2nd one.

thnx in advance
sushant

推荐答案

请不要在CLC中询问面试问题。 ..

Please dont ask interview questions in CLC....


sushant< th ******** @ rediffmail.com>写道:
sushant <th********@rediffmail.com> wrote:
假设我有2个循环一个在另一个里面,比如这个
1)for(i = 0; i< = 100; i ++)
{
for(j = 0; j< = 10; j ++)
{
一些代码;
}
}
2)for(i = 0; i< = 10; i ++)
{
for(j = 0; j< = 100; j ++)
{
一些代码;
}
}
所以哪个循环在第一个或第二个循环上工作得更快。
suppose i have 2 loops one inside the other, like this 1) for(i=0;i<=100;i++)
{
for(j=0;j<=10;j++)
{
some code;
}
} 2) for(i=0;i<=10;i++)
{
for(j=0;j<=100;j++)
{
some code;
}
} so which loops will work faster the 1st one or the 2nd one.




经过仔细测量后,没有人可以告诉你。在C语言的sppecifications中没有任何东西需要

其中一个循环要快于另一个循环。如果其中一个应该比它更快,那么由于实现的方式,即C com-

piler,写的也可能取决于什么你在循环中做。

你只能通过衡量发生的事情来找出答案。不幸的是,

结果只对您所做的编译器/系统组合有效

测试时。

问候,Jens
-

\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de

\ __________________________ http://www.toerring.de



Nobody can tell but you after you made careful measurements. There''s
nothing in the sppecifications of the C language that would require
one of the loops to be faster than the other. If one of them should
be faster than it''s due to the way the implementation, i.e. the C com-
piler, is written and may also depend on what you''re doing in the loop.
You can only find out by measuring what happens. Unfortunately, that
result will only be valid for the compiler/system combination you did
the tests with.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


它将真正依赖于某些代码。

如果是某些代码只是适合缓存,但外部循环不是:

(2)会更快。


如果内部和外部循环适合缓存,我不指望



a性能差异很大。


Deepa

-
http://www.EventHelix.com/EventStudio

EventStudio 2.5 - 自动生成序列图

It will really depend upon "some code".
If "some code" just fits into the cache but the outer loop doesn''t:
(2) would be faster.

If the inner and the outer loop fit into the cache, I don''t expect to
see
a large difference in performance.

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Automate sequence diagram generation


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

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