如何避免for()的循环开销,而() [英] how to avoid loop overhead of for(), while()

查看:119
本文介绍了如何避免for()的循环开销,而()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算矩阵乘法码(在MFLOPS中)。我想运行

代码100,000次或其他一些大号。

这可以通过2种方式完成(for和while循环):

timer1 = time(NULL);

for(n = 0; n< 100000; n ++)

mm();

timer2 =时间(NULL);


exectime = difftime(timer2,timer1);


但是,这意味着会有n的比较每次每次

迭代和增量操作。我想避免这种情况,如果

你使用while()同样的问题。

所以有一些方法可以避免循环开销,即告诉它:

做100000次{

mm

}

Pushkar Pradhan

解决方案

Pushkar Pradhan写道:

timer1 = time(NULL);
for(n = 0; n< 100000; n ++)
mm();
timer2 = time(NULL);

exectime = difftime(timer2,timer1);

然而,这意味着将会对n进行比较。每次每次迭代和增量操作。我想避免这种情况,如果
你使用while()相同的问题。
那么有些方法可以避免循环开销,即告诉它:
做100000次{
mm
}
Pushkar Pradhan




ehm ......你觉得这个陈述到底是怎么实现

如果除了保持一个计数器初始化为100000并且

递减并且为每个循环测试等于0时存在?你知道

我们其他人想念的任何魔法吗?


- 核/实验室 -


Pushkar Pradhan写道:

我想把我的矩阵乘以代码(在MFLOPS中)。我想运行
代码100,000次或其他一些大号。
这可以通过2种方式完成(for和while循环):

timer1 = time(NULL);
for(n = 0; n< 100000; n ++)
mm();
timer2 = time(NULL);

exectime = difftime(timer2, timer1);

然而,这意味着将会有n的比较。每次每次迭代和增量操作。我想避免这种情况,如果你使用while()同样的问题。


一个好的编译器应该(并且通常是)足够智能,以便了解比较和增量并优化它。即使它没有b $ b,这些比较和整数增量也几乎没有占用额外的CPU周期,与你的mm()函数相比需要额外的CPU周期
(翻牌将需要更多周期,所以当一次翻牌正在进行时,

处理器将能够进行多次整数操作和

比较)。实际上,使用

循环来计算你的功能会有* * *开销。字面上*所有*科学代码那里

在那里使用循环来测量函数的性能。你好b / b
根本不应该担心管理费用。


因此有一些方法可以避免循环开销,即告诉它:
做100000时间{
mm
}


即使有办法做到这一点,这也需要隐含的

比较代码被执行的次数,以及每次增加它来保持计数的
因此具有相同的效果。


-Rick

Pushkar Pradhan




分别运行两个测量时间的例程。


1.第一例程

timer1 = time(NULL);

for(n = 0; n< 100000; n ++)

mm();

timer2 =时间(NULL);


time1 = difftime(timer2,timer1);


2.第二个例程 - n必须用volatile类型限定符声明

timer1 = time(NULL);

for(n = 0; n< 100000; n ++);


timer2 =时间(NULL);


time2 = difftime(timer2,timer1);


可以计算调用mm()100000次的净时间....

exectime = time2 - time1。


但每次运行时结果可能会有所不同。

" Pushkar Pradhan < PU ******* @ hotmail.com>在消息中写道

新闻:7f ************************* @ posting.google.co m ...

:我想计算矩阵乘法码(在MFLOPS中)。我想运行

:代码100,000次或其他一些大号。

:这可以通过2种方式完成(for和while循环):



:timer1 = time(NULL);

:for(n = 0; n< 100000; n ++)

:mm();

:timer2 = time(NULL);



:exectime = difftime(timer2,timer1);



:但是,这意味着将对n进行比较。在每个

:每次迭代和增量操作。我想避免这种情况,如果

:你使用while()同样的问题。

:那么有什么办法可以避免循环开销,即告诉它:

:做100000次{

:mm

:}

:Pushkar Pradhan


I want to time my matrix multiply code (in MFLOPS). I want to run the
code 100,000 times or some other big number.
This can be done 2 ways (for and while loops):

timer1 = time(NULL);
for(n = 0; n < 100000; n++)
mm();
timer2 = time(NULL);

exectime = difftime(timer2, timer1);

However, this means there will be a comparison of "n" on each
iteration and increment operation each time. I want to avoid this, if
you use while() same problem.
So is there someway to avoid the loop overhead, i.e. tell it:
do 100000 times{
mm
}
Pushkar Pradhan

解决方案

Pushkar Pradhan wrote:

timer1 = time(NULL);
for(n = 0; n < 100000; n++)
mm();
timer2 = time(NULL);

exectime = difftime(timer2, timer1);

However, this means there will be a comparison of "n" on each
iteration and increment operation each time. I want to avoid this, if
you use while() same problem.
So is there someway to avoid the loop overhead, i.e. tell it:
do 100000 times{
mm
}
Pushkar Pradhan



ehm... and how exactly do you think that statement would be implemented
if it existed apart from keeping a counter initialized to 100000 and
decremented and tested for equality with 0 for each loop? do you know of
any magic that the rest of us miss?

-- Nuclear / the Lab --


Pushkar Pradhan wrote:

I want to time my matrix multiply code (in MFLOPS). I want to run the
code 100,000 times or some other big number.
This can be done 2 ways (for and while loops):

timer1 = time(NULL);
for(n = 0; n < 100000; n++)
mm();
timer2 = time(NULL);

exectime = difftime(timer2, timer1);

However, this means there will be a comparison of "n" on each
iteration and increment operation each time. I want to avoid this, if
you use while() same problem.
A good compiler should be (and usually is) smart enough to know about
the comparisons and increments and optimize this away. Even if it
doesn''t, these comparisons and integer increments hardly ever take up
extra CPU cycles compared to what your mm() function will be taking
(flops will take more cycles and so while a single flop is underway, the
processor will be able to do a number of integer operations and
comparisons). Effectively, there will be *no* overhead involved in using
a loop for timing your function. Literally *all* scientific code there
is out there uses loops for measuring performance of functions. You
should have no concern about overheads at all.

So is there someway to avoid the loop overhead, i.e. tell it:
do 100000 times{
mm
}
Even if there was a way to do this, this would require an implicit
comparison on the number of times the code has been executed, and
keeping a count by incrementing it each time, thus having the same effect.

-Rick

Pushkar Pradhan




Run two routines measuring the time respectively.

1. first routine
timer1 = time(NULL);
for(n = 0; n < 100000; n++)
mm();
timer2 = time(NULL);

time1 = difftime(timer2, timer1);

2. second routine - n must be declared with the volatile type qualifier
timer1 = time(NULL);
for(n = 0; n < 100000; n++);

timer2 = time(NULL);

time2 = difftime(timer2, timer1);

The net time to call mm() 100000 times could be calculated....
exectime = time2 - time1.

But the results may differ every time you run.
"Pushkar Pradhan" <pu*******@hotmail.com> wrote in message
news:7f*************************@posting.google.co m...
: I want to time my matrix multiply code (in MFLOPS). I want to run the
: code 100,000 times or some other big number.
: This can be done 2 ways (for and while loops):
:
: timer1 = time(NULL);
: for(n = 0; n < 100000; n++)
: mm();
: timer2 = time(NULL);
:
: exectime = difftime(timer2, timer1);
:
: However, this means there will be a comparison of "n" on each
: iteration and increment operation each time. I want to avoid this, if
: you use while() same problem.
: So is there someway to avoid the loop overhead, i.e. tell it:
: do 100000 times{
: mm
: }
: Pushkar Pradhan


这篇关于如何避免for()的循环开销,而()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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