如何使这个代码更快(学习最佳实践)? [英] How to make this code faster (learning best practices)?
问题描述
我有这个小圈子,我想知道如果我做一些大错误,perf明智。
例如,是否有一种方法来重写它的不同部分,使矢量化可能(假设GCC4.8.1和所有vecotrization友好标志启用)?
这是传递列表数字的最好方法(const float name_of_var [])?
代码的想法是从(未排序的数字) y
和两个绑定值(<$ c)采用向量(在数学意义上,不必要的std :: vector) $ c> ox [0] <= ox [1] )并存储在整数向量 rdx
满足ox [0] <= y [i] <= ox [1]的条目的 y
p>
rdx
可以包含 m
元素和 y
有能力 n
和 n> m
。如果有多于 m
个值 y [i]
满足ox [0] y [i] <= ox [1],则代码应返回第一个 m
,
void foo(const int n,const int m,const float y [],const float ox [],int rdx []){
int d0,j = 0,i = 0;
for(;;){
i ++;
d0 =((y [i] = ox [0])+(y [i] <= ox [1]))/ 2;
if(d0 == 1){
rdx [j] = i;
j ++;
}
if(j == m)break;
if(i == n-1)break;
}
}
p> d0 =((y [i] = ox [0])+(y [i] <= ox [1]
if(d0 == 1)
我相信使用中间变量是无用的,并再花几个周期
这是我能想到的最优化的版本,但它是完全不可读...
void foo(int n,int m,float y [],const float ox [],int rdx [])
{ int i = 0; i {
if(* y> = * ox&& * y <= ox [1 ])
{
* rdx = i;
rdx ++;
m--;
}
y ++;
}
}
我认为以下版本有一个体面的优化标志do the job
void foo(int n,int m,const float y [],const float ox [],int rdx [])
{
for(int j = 0,i = 0; j {
if(y [i]> = ox [0]& y [i]< = ox [1])$ b $ b {
rdx [j ++] = i;
}
}
}
I have this little loop here, and I was wondering if I do some big mistake, perf wise.
For example, is there a way to rewrite parts of it differently, to make vectorization possible (assuming GCC4.8.1 and all vecotrization friendly flags enabled)?
Is this the best way to pass a list a number (const float name_of_var[])?
The idea of the code is to take a vector (in the mathematical sense, not necesserly a std::vector) of (unsorted numbers) y
and two bound values (ox[0]<=ox[1]
) and to store in a vector of integers rdx
the index i
of the entry of y
satisfying ox[0]<=y[i]<=ox[1].
rdx
can contain m
elements and y
has capacity n
and n>m
. If there are more than m
values of y[i]
satisfying ox[0]<=y[i]<=ox[1] then the code should return the first m
Thanks in advance,
void foo(const int n,const int m,const float y[],const float ox[],int rdx[]){
int d0,j=0,i=0;
for(;;){
i++;
d0=((y[i]>=ox[0])+(y[i]<=ox[1]))/2;
if(d0==1){
rdx[j]=i;
j++;
}
if(j==m) break;
if(i==n-1) break;
}
}
d0=((y[i]>=ox[0])+(y[i]<=ox[1]))/2;
if(d0==1)
I believe the use of an intermediary variable is useless, and take a few more cycles
This is the most optimized version I could think of, but it's totally unreadable...
void foo(int n, int m, float y[],const float ox[],int rdx[])
{
for(int i = 0; i < n && m != 0; i++)
{
if(*y >= *ox && *y <= ox[1])
{
*rdx=i;
rdx++;
m--;
}
y++;
}
}
I think the following version with a decent optimisation flag should do the job
void foo(int n, int m,const float y[],const float ox[],int rdx[])
{
for(int j = 0, i = 0; j < m && i < n; i++) //Reorder to put the condition with the highest probability to fail first
{
if(y[i] >= ox[0] && y[i] <= ox[1])
{
rdx[j++] = i;
}
}
}
这篇关于如何使这个代码更快(学习最佳实践)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!