如何让它更快? [英] How to make it faster?

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

问题描述

当我使用gprof查看哪个功能消耗最多的运行时间时,我确认了下面的一个。 sz平均不到5000,但是
foo被称为大约1,000,000次。我尝试过使用

" register sum = 0.0"并看到了一些改进。我的问题是如何使b $ b进一步提高foo以使其更快。


double foo(double * a,double * b,int sz)

{


double sum = 0.0;

int i;


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

总和+ = a [i] * b [i];


返还金额;

}

When I used gprof to see which function consumed most running time, I
identified the following one. sz was less than 5000 on average, but
foo had been called about 1,000,000 times. I have tried using
"register sum = 0.0" and saw some improvement. My question is how to
improve foo further to make it faster.

double foo(double *a, double *b, int sz)
{

double sum = 0.0;
int i;

for (i = 0; i < sz; i++)
sum += a[i]*b[i];

return sum;
}

推荐答案




istillsh ... @ gmail.com写道:


istillsh...@gmail.com wrote:

当我使用gprof查看哪个函数占用了大部分运行时间时,我确定了以下一个函数。 sz平均不到5000,但是
foo被称为大约1,000,000次。我尝试过使用

" register sum = 0.0"并看到了一些改进。我的问题是如何使b $ b进一步提高foo以使其更快。


double foo(double * a,double * b,int sz)

{


double sum = 0.0;

int i;


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

总和+ = a [i] * b [i];


返还金额;

}
When I used gprof to see which function consumed most running time, I
identified the following one. sz was less than 5000 on average, but
foo had been called about 1,000,000 times. I have tried using
"register sum = 0.0" and saw some improvement. My question is how to
improve foo further to make it faster.

double foo(double *a, double *b, int sz)
{

double sum = 0.0;
int i;

for (i = 0; i < sz; i++)
sum += a[i]*b[i];

return sum;
}



两倍的速度会有很大的帮助。

Twice faster would help a lot.




<是********* @ gmail.com写的消息

<is*********@gmail.comwrote in message

double foo(double * a,double * b,int sz)

{


double sum = 0.0;

int i;


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

sum + = a [i] * b [i];


返还金额;

}
double foo(double *a, double *b, int sz)
{

double sum = 0.0;
int i;

for (i = 0; i < sz; i++)
sum += a[i]*b[i];

return sum;
}



制作a和b浮点数组,并对浮点数求和。在一些架构上,

将提供大约2倍的加速。当然,你必须在整个程序中使用单精度



代码很简单,不太可能有任何其他类型的
您可以做
微优化。但是可以通过重新排列算法来减少调用foo()




-

免费游戏和编程好东西。
http://www.personal.leeds。 ac.uk/~bgy1mm

Make a and b float arrays, and sum a float too. On some architectures that
will give about a 2 * speedup. Of course you''ll have to use single precision
throughout the program.
The code is so simple that there is unlikely to be any sort of other
micro-optimisiation you can do. However it may be possible to call foo()
less often by rearranging the algorithm.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm




< is ********* @ gmail.com写信息

新闻:6d ********************************** @ k10g2000 prm.googlegroups.com ...

<is*********@gmail.comwrote in message
news:6d**********************************@k10g2000 prm.googlegroups.com...

>


istillsh ... @ gmail.com写道:
>

istillsh...@gmail.com wrote:

>当我使用gprof查看哪个功能消耗最多的运行时间时,我确定了以下内容。 sz平均不到5000,但是foo被称为大约1,000,000次。我尝试过使用
register sum = 0.0并看到了一些改进。我的问题是如何进一步提高foo以使其更快。

double foo(double * a,double * b,int sz)
{

double sum = 0.0;
int i;

for(i = 0; i< sz; i ++)
sum + = a [i] * b [i ];

返回总和;
}
>When I used gprof to see which function consumed most running time, I
identified the following one. sz was less than 5000 on average, but
foo had been called about 1,000,000 times. I have tried using
"register sum = 0.0" and saw some improvement. My question is how to
improve foo further to make it faster.

double foo(double *a, double *b, int sz)
{

double sum = 0.0;
int i;

for (i = 0; i < sz; i++)
sum += a[i]*b[i];

return sum;
}



两倍的速度会有很大帮助。


Twice faster would help a lot.



已经建议改善循环。


减少1000000的呼叫将有助于更多,需要关注整体

策略。


然后就是数据:sz需要那么高吗?有什么关于a和b数据(比如大多数是零或者一些)的特殊情况吗?


总和必须有多准确?如果a,b变化缓慢,或许每一对总和只有
(然后结果加倍)?


您是否考虑过使用整数(定点) ?这可能需要一个

的全球变化。


-

Bart

Suggestions have already been made for improving the loop.

Reducing the calls from 1000000 will help a lot more, for that the overall
strategy needs to be looked at.

Then there''s the data: does sz need to be that high? Is there anything
special about the a and b data (like mostly zeros or ones)?

How accurate does the sum have to be? If a,b change slowly, perhaps sum only
every other pair (then double the result)?

Have you thought about using integers (fixed point)? This might need a
global change.

--
Bart


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

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