嗨,我该如何优化以下代码? [英] Hi, how can I optimize the following code?

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

问题描述

我试图使用多个线程来优化我的以下代码,但是

遇到了一些问题,有人可以帮助我吗?


k [3] [初始化。


int computePot(){

int i,j;


for(i = 0; i <500; i ++){

for(j = 0; j< i-1; j ++){

distx = pow((r [0] [j] - r [0] [i]),2);

disty = pow((r [1] [j] - r [1] [i]),2);

distz = pow((r [2] [j] - r [2] [i]),2);

dist = sqrt(distx + disty + distz) ;

pot + = 1.0 / dist;

}

}


我怀疑如果这个代码真的可以优化吗?

I was trying to use multiple thread to optimize my following code, but
met some problems, anyone can help me?

k[3][500] are initialized.

int computePot() {
int i, j;

for( i=0; i<500; i++ ) {
for( j=0; j<i-1; j++ ) {
distx = pow( (r[0][j] - r[0][i]), 2 );
disty = pow( (r[1][j] - r[1][i]), 2 );
distz = pow( (r[2][j] - r[2][i]), 2 );
dist = sqrt( distx + disty + distz );
pot += 1.0 / dist;
}
}

I doubt that If this code really can be optimized?

推荐答案



kenneth写道:

kenneth wrote:

我试图使用多个线程来优化我的下列代码,但是

遇到了一些问题,有人可以帮助我吗?


k [3] [500]被初始化。


int computePot(){

int i,j;


for(i = 0; I< 500; i ++){

for(j = 0; j< i-1; j ++){

distx = pow((r [0] [j] - r [0 ] [i]),2);

disty = pow((r [1] [j] - r [1] [i]),2);

distz = pow((r [2] [j] - r [2] [i]),2);

dist = sqrt(distx + disty + distz);

pot + = 1.0 / dist;

}

}


我怀疑如果这段代码真的可以优化?
I was trying to use multiple thread to optimize my following code, but
met some problems, anyone can help me?

k[3][500] are initialized.

int computePot() {
int i, j;

for( i=0; i<500; i++ ) {
for( j=0; j<i-1; j++ ) {
distx = pow( (r[0][j] - r[0][i]), 2 );
disty = pow( (r[1][j] - r[1][i]), 2 );
distz = pow( (r[2][j] - r[2][i]), 2 );
dist = sqrt( distx + disty + distz );
pot += 1.0 / dist;
}
}

I doubt that If this code really can be optimized?



你可以经常通过提出一个非O(N ^ N
算法来优化O(N ^ 2)运算。 2)。编译器将命中你的循环和

优化它们相当多但这并没有解决

的问题,算法是一个N的平方.Don'不知道你是什么

试图这样做。你可能会在comp.programming中描述这个问题和

看看他们是否有一些想法。


换句话说,看你正在使用的算法而不是方法

微优化。

You can quite often optimize an O(N^2) operation by coming up with an
algorithm that is not O(N^2). The compiler will hit your loops and
optimize them quite a bit but that doesn''t resolve the problem of
having an algorithm that is a square of N. Don''t know what you are
trying to do. You might describe the problem in comp.programming and
see if they have some ideas.

In other words, look to the algorithms you are using not ways to
micro-optimize.


kenneth kirjoitti:
kenneth kirjoitti:

int computePot(){

int i,j;


for(i = 0; i< 500; i ++){

for(j = 0; j< i-1; j ++){

distx = pow((r [0] [j] - r [0] [i]),2);

disty = pow((r [1] [j] - r [1] [i]),2);

distz = pow((r [2] [j] - r [2] [i]),2);

dist = sqrt(distx + disty + distz);

pot + = 1.0 / dist;

}

}


我怀疑如果这段代码真的可以优化吗?
int computePot() {
int i, j;

for( i=0; i<500; i++ ) {
for( j=0; j<i-1; j++ ) {
distx = pow( (r[0][j] - r[0][i]), 2 );
disty = pow( (r[1][j] - r[1][i]), 2 );
distz = pow( (r[2][j] - r[2][i]), 2 );
dist = sqrt( distx + disty + distz );
pot += 1.0 / dist;
}
}

I doubt that If this code really can be optimized?



我不能说如何优化多线程但

不使用pow函数来计算平方。

函数pow需要t他指数为浮点数,它可能使用指数和对数函数



使用类似

--- -

内联双SquareDouble(双r)

{

返回r * r;

}

----



----

#define square(x)((x)*( x))

----

如果使用GNU C,请考虑使用const属性:

----

inline double SquareDouble(double r)__ attribute __((const))

{

return r * r;

}

----

-

Tommi H?yn?l?nmaa

s?hk?posti / e-mail:转到********* ****** @ iki.fi

kotisivu / homepage: http://www.iki.fi/tohoyn/


kenneth写道:
kenneth wrote:

我试图使用多个线程来选择模仿我的下面的代码,但

遇到了一些问题,有人可以帮助我吗?
I was trying to use multiple thread to optimize my following code, but
met some problems, anyone can help me?



我会尝试使用单个线程。多个线程需要

特定于平台的解决方案,因此超出了这个NG的范围。

I''ll try with a single thread. Multiple threads would require
platform-specific solutions, so are out of the scope of this NG.


k [3] [500]初始化。
k[3][500] are initialized.



What''sk?

What''s k?


int computePot(){

int i,j;


for(i = 0; i< 500; i ++){

for(j = 0; j< i-1; j ++){

distx = pow((r [0] [j] - r [0] [i]),2);

disty = pow((r [1] [j] - r [1] [i]),2);

distz = pow((r [2] [j] - r [2] [i]),2 );

dist = sqrt(distx + disty + distz);

pot + = 1.0 / dist;

}

}


我怀疑如果这段代码真的可以优化?
int computePot() {
int i, j;

for( i=0; i<500; i++ ) {
for( j=0; j<i-1; j++ ) {
distx = pow( (r[0][j] - r[0][i]), 2 );
disty = pow( (r[1][j] - r[1][i]), 2 );
distz = pow( (r[2][j] - r[2][i]), 2 );
dist = sqrt( distx + disty + distz );
pot += 1.0 / dist;
}
}

I doubt that If this code really can be optimized?



当然可以。首先忘记''pow''的事情。

要获得正方形,只需将数值乘以它。

这肯定会让它更快。


另一件事是你不需要索引内部循环中第二个向量的
,你可以作为

好吧在外面做,比如:


for(i){

寄存器double r0i = r [0] [i]; //现在仅评估500次

寄存器double r1i = r [1] [i];

寄存器double r2i = r [2] [i];

for(j){

distx =(r [0] [j] -r0i)*(r [0] [j] -r0i);

// ...

}

}


这应该会给你的代码带来提升。


HTH,

- J.

Sure it can. First of all forget that ''pow'' thing.
To get the square, just multiply the value by itself.
This will definitely make it much faster.

Another thing is that you don''t need to index out
the second vector inside the inner loop, you can as
well do it outside, like in:

for(i) {
register double r0i=r[0][i]; // now evaluated only 500 times
register double r1i=r[1][i];
register double r2i=r[2][i];
for(j) {
distx = (r[0][j]-r0i)*(r[0][j]-r0i);
// ...
}
}

That should give your code a boost.

HTH,
- J.


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

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