浮点算法很慢 [英] float algorithm is slow

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

问题描述

浮动百分比;


for(j = 0; j <10000000; j ++)

{

percentage = sinf(频率* j * 2 * 3.14159 / sampleFreq);

缓冲[totalBytes] = ceilf(音量*百分比)+音量;

totalBytes ++;

}


因为浮点变量,上面的循环在Linux机器上需要2秒c或c ++

。有没有人有办法减少时间?


谢谢,


文斐

解决方案

Wenfei写道:

浮动百分比;

(j = 0; j <10000000; j ++ ){
百分比= sinf(频率* j * 2 * 3.14159 / sampleFreq);
缓冲区[totalBytes] = ceilf(音量*百分比)+音量;
totalBytes ++;
}

因为float变量,上面的循环在Linux机器上需要2秒c或c ++
。有没有人有减少时间的解决方案?
cat main.c
#include< stdlib.h>

#include< math.h>


int main (int argc,char * argv []){


const

size_t n = 10000000;

float buffer [n] ;

size_t totalBytes = 0;

const

float_t frequency = 1.0;

const

float_t sampleFreq = 1.0;

const

float_t pi = 3.14159265358979323846;

const

float_t volume = 1.0;


for(size_t j = 0; j< n; ++ j){

float_t percentage = sinf(频率* j * 2 * pi / sampleFreq);

buffer [totalBytes] = ceilf(音量*百分比)+音量;

totalBytes ++;

}


返回0;

}

gcc -Wall -std = c99 -pedantic -O2 -o main main.c -lm
时间./main



3.694u 0.258s 0:03.92 100.5%0 + 0k 0 + 0io 0pf + 0w


文斐写道:浮动百分比;

(j = 0; j<千万; j ++)
{百分比= sinf(频率* j * 2 * 3.14159 / sampleFreq);
缓冲区[totalBytes] = ceilf(音量*百分比)+音量;
totalBytes ++;
}

因为float变量,上面的循环在Linux机器上的c或c ++中需要2秒钟。有没有人有减少时间的解决方案?




是的:将迭代次数从10000000更改为0,并且

代码几乎可以肯定运行得更快。


换句话说,这种微基准测试并不是非常有用的。你真的想做什么?


-

Eric Sosman
es ***** @ acm-dot-org.inva lid


>浮动百分比;


for(j = 0; j <10000000; j ++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq);
buffer [totalBytes] = ceilf(音量*百分比)+音量;
totalBytes ++;
}
因为浮点变量,


我的猜测是,如果你获得浮动变量的RID,那么它将需要大约相同的时间:

for(j = 0; j <10000000; j ++ )
{

buffer [totalBytes] = ceilf(volume * sinf(frequency * j * 2 * 3.14159 / sampleFreq))+ volume;

totalBytes ++;

}

在Linux机器上,上述循环在c或c ++中花费2秒钟。有没有人有减少时间的解决方案?




你还没有证明为什么花两秒钟是个问题。

Cut减少迭代次数?获得更快的机器?

以双倍计算可能会使它更快(尽管在

Intel * 86上它可能不会)。


Gordon L. Burditt


float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?

Thanks,

Wenfei

解决方案

Wenfei wrote:

float percentage;

for (j = 0; j < 10000000; j++) {
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time? cat main.c #include <stdlib.h>
#include <math.h>

int main(int argc, char* argv[]) {

const
size_t n = 10000000;
float buffer[n];
size_t totalBytes = 0;
const
float_t frequency = 1.0;
const
float_t sampleFreq = 1.0;
const
float_t pi = 3.14159265358979323846;
const
float_t volume = 1.0;

for (size_t j = 0; j < n; ++j) {
float_t percentage = sinf(frequency*j*2*pi/sampleFreq);
buffer[totalBytes] =ceilf(volume*percentage) + volume;
totalBytes++;
}

return 0;
}
gcc -Wall -std=c99 -pedantic -O2 -o main main.c -lm
time ./main


3.694u 0.258s 0:03.92 100.5% 0+0k 0+0io 0pf+0w


Wenfei wrote:

float percentage;

for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable, the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?



Yes: Change the iteration count from 10000000 to 0, and
the code will almost certainly run faster.

In other words, micro-benchmarks of this sort are not
very informative. What are you really trying to do?

--
Eric Sosman
es*****@acm-dot-org.invalid


>float percentage;


for (j = 0; j < 10000000; j++)
{
percentage = sinf(frequency * j * 2 * 3.14159 / sampleFreq );
buffer[totalBytes] =ceilf(volume * percentage) + volume;
totalBytes++;
}

Because the float variable,
My guess is that if you GOT RID OF the float variable, it would
take about the same time:
for (j = 0; j < 10000000; j++) {
buffer[totalBytes] =ceilf(volume * sinf(frequency * j * 2 * 3.14159 / sampleFreq )) + volume;
totalBytes++;
}
the above loop take 2 seconds in c or c++
on Linux machine. Does anybody has a solution to reduce the time?



You haven''t demonstrated why taking two seconds is a problem yet.
Cut down the number of iterations? Get a faster machine?
Doing the calculation in double might make it faster (although on
Intel *86 it probably won''t).

Gordon L. Burditt


这篇关于浮点算法很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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