需要优化 [英] Optimisation needed

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

问题描述




我的内循环运行得太慢了。我已经尽力优惠了

优化它,但我并没有真正体验到什么是最好的。可以

有人建议任何优化吗?


感谢您的帮助!

for(m = -range; m< = range ; m ++)//范围是1或2

{

for(n = -range; n< = range; n ++)

{

/ * indices * /

int x0 =(int)px - m;

int y0 =(int)py - n;

index = x0 * kpar-> fftrows + y0;


/ *零越界* /

指数* =(int)(index> = 0&& index<(kpar-> fftrows * kpar-> fftrows));


/ *计算距离* /

float dx = px - (float)x0;

float dy = py - (float)y0;

deltad = dx * dx + dy * dy;


/ *计算系数* /

fscale = expf(deltad * norm);


/ *商店价值* /

str-> array_1 [index] + = val_1 * fscale;

str-> array_2 [index] + = val_2 * fscale;

str-> array_3 [index] + = fscale;

}

}

解决方案



" spasmous" < SP ****** @ yahoo.com>在留言新闻中写道:2f ************************** @ posting.google.c om ...



我有一个运行得太慢的内循环。我已经尽力优化它,但我并没有真正体验到最好的方法。可以
任何人建议任何优化吗?




如果你告诉我们某些类型的东西,比如px,py

和array_1,2,3。


此外,如果您使用的是Pentium芯片,您可能希望以具体情况询问

新闻组。大多数编译器为float / double生成绝对可怕的代码

到int转换。


sp ****** @ yahoo.com (spasmous)在新闻中写道:2f066762.0401261506.2c3522a2

@ posting.google.com:



我的内循环运行得太慢了。我已经尽力优化它,但我并没有真正体验到最好的方法。可以
任何人建议任何优化吗?

感谢您的帮助!

for(m = -range; m< = range; m ++)//范围要么是1或2
对于(n = -range; n< = range; n ++)
{
/ * indices * /
int x0 =(int) px - m;
int y0 =(int)py - n;
index = x0 * kpar-> fftrows + y0;

/ *零越界* /
index * =(int)(index> = 0&& index<(kpar-> fftrows * kpar-> fftrows));

/ *计算距离* /
float dx = px - (float)x0;
float dy = py - (float)y0;
deltad = dx * dx + dy * dy;

/ *计算系数* /
fscale = expf(deltad * norm);

/ *存储值* /
str-> array_1 [index] + = val_1 * fscale ;
str-> array_2 [index] + = val_2 * fscale;
str-> array_3 [index] + = fscale;
}
}




嗯......一些简单的东西_might_帮助:


Mo内部循环外的x0的声明和初始化。它每次都不需要重新计算


在内循环外移动dx的声明和初始化

(同样的原因)。我也可能把它放在循环外面。


spasmous写道:



我有一个运行得太慢的内循环。我已经尽力优化它,但我并没有真正体验到最好的方法。可以
任何人建议任何优化吗?

感谢您的帮助!

for(m = -range; m< = range; m ++)//范围要么是1或2
对于(n = -range; n< = range; n ++)
{
/ * indices * /
int x0 =(int) px - m;
int y0 =(int)py - n;
index = x0 * kpar-> fftrows + y0;

/ *零越界* /
index * =(int)(index> = 0&& index<(kpar-> fftrows * kpar-> fftrows));

/ *计算距离* /
float dx = px - (float)x0;
float dy = py - (float)y0;


让我们执行一些替代:

给定X0 == px - m;从上面,

dx = px - (px - m);

dx = px - px + m;

dx = m;

与dy类似:

dy = n;

deltad = dx * dx + dy * dy;


这简化为:

deltad = m * m + n * n;

此简化删除变量dx和dy。


/ *计算系数* /
fscale = expf(deltad * norm);

/ *存储值* /
str-> array_1 [index] + = val_1 * fscale;
str-> array_2 [index] + = val_2 * fscale;
str-> array_3 [index] + = fscale;
}
}




-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net /~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++常见问题:
http://www.raos。 demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/stl - 标准模板库


Hi,

I have an inner loop that is running too slowly. I''ve done my best at
optimising it but am not really experienced at what works best. Can
anyone suggest any optimisations?

Thanks for any help!
for(m=-range;m<=range;m++) // range is either 1 or 2
{
for(n=-range;n<=range;n++)
{
/* indices */
int x0 = (int)px - m;
int y0 = (int)py - n;
index = x0*kpar->fftrows + y0;

/* zero out of bounds */
index *= (int)(index>=0 && index<(kpar->fftrows*kpar->fftrows));

/* calculate distance */
float dx = px - (float)x0;
float dy = py - (float)y0;
deltad = dx*dx + dy*dy;

/* calculate coefficient */
fscale = expf(deltad * norm);

/* store values */
str->array_1[index] += val_1 * fscale;
str->array_2[index] += val_2 * fscale;
str->array_3[index] += fscale;
}
}

解决方案


"spasmous" <sp******@yahoo.com> wrote in message news:2f**************************@posting.google.c om...

Hi,

I have an inner loop that is running too slowly. I''ve done my best at
optimising it but am not really experienced at what works best. Can
anyone suggest any optimisations?



It would be ncie if you told us the types of some of these things like px, py
and array_1,2,3.

Also if you''re on a Pentium chip, you may wish to ask in a intel specific
newsgroup. Most compilers generate absolutely horrific code for float/double
to int conversion.


sp******@yahoo.com (spasmous) wrote in news:2f066762.0401261506.2c3522a2
@posting.google.com:

Hi,

I have an inner loop that is running too slowly. I''ve done my best at
optimising it but am not really experienced at what works best. Can
anyone suggest any optimisations?

Thanks for any help!
for(m=-range;m<=range;m++) // range is either 1 or 2
{
for(n=-range;n<=range;n++)
{
/* indices */
int x0 = (int)px - m;
int y0 = (int)py - n;
index = x0*kpar->fftrows + y0;

/* zero out of bounds */
index *= (int)(index>=0 && index<(kpar->fftrows*kpar->fftrows));

/* calculate distance */
float dx = px - (float)x0;
float dy = py - (float)y0;
deltad = dx*dx + dy*dy;

/* calculate coefficient */
fscale = expf(deltad * norm);

/* store values */
str->array_1[index] += val_1 * fscale;
str->array_2[index] += val_2 * fscale;
str->array_3[index] += fscale;
}
}



Umm... some simple stuff that _might_ help:

Move the declaration and initialization of x0 outside the inner loop. It
doesn''t need to be recalculated every time.

Move the declaration and initialization of dx outside the inner loop
(same reason). I''d probably square it outside the loop too.


spasmous wrote:

Hi,

I have an inner loop that is running too slowly. I''ve done my best at
optimising it but am not really experienced at what works best. Can
anyone suggest any optimisations?

Thanks for any help!
for(m=-range;m<=range;m++) // range is either 1 or 2
{
for(n=-range;n<=range;n++)
{
/* indices */
int x0 = (int)px - m;
int y0 = (int)py - n;
index = x0*kpar->fftrows + y0;

/* zero out of bounds */
index *= (int)(index>=0 && index<(kpar->fftrows*kpar->fftrows));

/* calculate distance */
float dx = px - (float)x0;
float dy = py - (float)y0;
Let us perform some substition:
Given X0 == px - m; from above,
dx = px - (px - m);
dx = px - px + m;
dx = m;
Similarly with dy:
dy = n;
deltad = dx*dx + dy*dy;
This is simplified to:
deltad = m * m + n * n;
This simplification removes the variables dx and dy.

/* calculate coefficient */
fscale = expf(deltad * norm);

/* store values */
str->array_1[index] += val_1 * fscale;
str->array_2[index] += val_2 * fscale;
str->array_3[index] += fscale;
}
}



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library


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

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