移除循环导致它以一半的速度移动? [英] removing a loop cause it to go at half the speed?

查看:46
本文介绍了移除循环导致它以一半的速度移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在对数学的速度做一个简单的测试。操作当我测试它时,我发现删除初始化数据阵列的循环

的操作导致整个程序花费两倍的时间来

完成。如果包含循环,完成后需要大约7.48秒才能完成,但是当删除它需要大约11.48秒。


是否有人建议为什么这是如此,我是否可以

相信代码的结果如下所示?


/ tom

代码在Linux 2.6.3上使用gcc 3.3.2和glibc 2.2.3编译

#include< stdio.h>

#include< stdlib.h>

#include< sys / time.h>


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

{

int total = 0;

int count = 65500;

signed int data [count];


/ *初始化数组循环* /

for(int c = 0; c< count; c ++){

data [c] = 1 +(int) (2000000000.0 * rand()/(RAND_MAX + 1.0));

}


struct timeval start_time;

struct timeval end_time ;


gettimeofday(& start_time,NULL);


for(int d = 0; d< 50000; d ++){

for(int c = 0; ℃下计数; c ++){

总计+ =数据[c];

}

}

gettimeofday(& end_time ,NULL);


double t1 =(start_time.tv_sec * 1000)+(start_time.tv_usec / 1000.0);

double t2 =(end_time.tv_sec * 1000)+(end_time.tv_usec / 1000.0);


printf(经过时间(毫秒):%。6lf \ n,t2-t1);

printf(总计:%u \ n,总计);


返回(0);

}

Hi

I was doing a simple test of the speed of a "maths" operation and when I
tested it I found that removing the loop that initialises the data array
for the operation caused the whole program to spend twice the time to
complete. If the loop is included it takes about 7.48 seconds to
complete, but when removed it takes about 11.48 seconds.

Does anybody have a suggestion as to why this is so and whether I can
trust the results of the code as it is below?

/tom
The code was compiled on linux 2.6.3 with gcc 3.3.2 and glibc 2.2.3
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

int main(int argc, char *argv[])
{
int total = 0;
int count = 65500;
signed int data[count];

/* Initialising array loop */
for(int c=0; c<count; c++) {
data[c]=1+(int) (2000000000.0*rand()/(RAND_MAX+1.0));
}

struct timeval start_time;
struct timeval end_time;

gettimeofday(&start_time, NULL);

for(int d=0; d<50000; d++) {
for(int c=0; c<count; c++) {
total += data[c];
}
}
gettimeofday(&end_time, NULL);

double t1=(start_time.tv_sec*1000)+(start_time.tv_usec/1000.0);
double t2=(end_time.tv_sec*1000)+(end_time.tv_usec/1000.0);

printf("Elapsed time (ms): %.6lf\n", t2-t1);
printf("Total: %u\n", total);

return(0);
}

推荐答案



" tom fredriksen" < to*@nospam.org>在留言中写道

news:44 ******** @ news.broadpark.no ...

"tom fredriksen" <to*@nospam.org> wrote in message
news:44********@news.broadpark.no...


我是对数学的速度进行简单的测试。操作,当我对它进行测试时,我发现删除为操作初始化数据阵列的循环导致整个程序花费两倍的时间来完成。如果包含循环,则需要大约7.48秒才能完成,但是当移除它需要大约11.48秒。

是否有人建议为什么会这样,以及我是否可以<相信代码的结果如下所示?

/ tom

代码是在linux 2.6.3上使用gcc 3.3.2和glibc 2.2编译的。 3

#include< stdio.h>
#include< stdlib.h>
#include< sys / time.h>
int main(int argc,char * argv [])
{int = total = 0;
int count = 65500;
signed int data [count];

/ *初始化数组循环* /
for(int c = 0; c< count; c ++){
data [c] = 1 +(int)(2000000000.0 * rand() /(RAND_MAX +1.0));
}
struct timeval start_time;
struct timeval end_time;

gettimeofday(& start_time,NULL);

for(int d = 0; d< 50000; d ++){
for(int c = 0; c< count; c ++){
total + = data [c ];
}
}
gettimeofday(& end_ti我,NULL);

double t1 =(start_time.tv_sec * 1000)+(start_time.tv_usec / 1000.0);
double t2 =(end_time.tv_sec * 1000)+(end_time。 tv_usec / 1000.0);

printf(经过时间(毫秒):%。6lf \ n,t2-t1);
printf(总计:%u \ n",total);

返回(0);
}
Hi

I was doing a simple test of the speed of a "maths" operation and when I
tested it I found that removing the loop that initialises the data array
for the operation caused the whole program to spend twice the time to
complete. If the loop is included it takes about 7.48 seconds to
complete, but when removed it takes about 11.48 seconds.

Does anybody have a suggestion as to why this is so and whether I can
trust the results of the code as it is below?

/tom
The code was compiled on linux 2.6.3 with gcc 3.3.2 and glibc 2.2.3
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

int main(int argc, char *argv[])
{
int total = 0;
int count = 65500;
signed int data[count];

/* Initialising array loop */
for(int c=0; c<count; c++) {
data[c]=1+(int) (2000000000.0*rand()/(RAND_MAX+1.0));
}

struct timeval start_time;
struct timeval end_time;

gettimeofday(&start_time, NULL);

for(int d=0; d<50000; d++) {
for(int c=0; c<count; c++) {
total += data[c];
}
}
gettimeofday(&end_time, NULL);

double t1=(start_time.tv_sec*1000)+(start_time.tv_usec/1000.0);
double t2=(end_time.tv_sec*1000)+(end_time.tv_usec/1000.0);

printf("Elapsed time (ms): %.6lf\n", t2-t1);
printf("Total: %u\n", total);

return(0);
}




猜一猜:数组数据[]是签名并且当将这些总和统一起来时,数据必须添加许多负值,这些负值可能比使用无符号值初始化数组时使用更多或更慢的指令更慢。

。 />
Rod Pemberton



Just a guess: the array data[] is signed and when unitialized the summation
of the data has to add many negative values which probably use more or
slower instructions than when you initialize the array with unsigned values.
Rod Pemberton


tom fredriksen写道:
tom fredriksen wrote:
。 。 。我发现删除为操作初始化数据阵列的循环导致整个程序花费两倍的时间来完成。 。 。 。有没有人建议为什么会这样?
。 。 。
for(int d = 0; d< 50000; d ++){
for(int c = 0; c< count; c ++){
total + = data [c];
}
}
. . . I found that removing the loop that initialises the data array
for the operation caused the whole program to spend twice the time to
complete. . . . Does anybody have a suggestion as to why this is so?
. . .
for(int d=0; d<50000; d++) {
for(int c=0; c<count; c++) {
total += data[c];
}
}




也许用于处理溢出中断的时间,以及

未初始化的数据[]给出了更多他们也许你的编译器

不够智能,可以用* = 50000

(两次)替换d循环。从源代码中总是很清楚

,其中测量的运行时间是花费的。

-



Maybe the time is spent servicing overflow interrupts, and the
uninitialised data[] gives more of them. Maybe your compiler
is not smart enough to replace the d-loop by total *= 50000
(both times). It is not always clear from the source code just
where the measured run time is being spent.
--

< br>

bert写道:
tom fredriksen写道:
tom fredriksen wrote:
。 。 。我发现删除为操作初始化数据阵列的循环导致整个程序花费两倍的时间来完成。 。 。 。有没有人建议为什么会这样?
。 。 。
for(int d = 0; d< 50000; d ++){
for(int c = 0; c< count; c ++){
total + = data [c];
}
}
. . . I found that removing the loop that initialises the data array
for the operation caused the whole program to spend twice the time to
complete. . . . Does anybody have a suggestion as to why this is so?
. . .
for(int d=0; d<50000; d++) {
for(int c=0; c<count; c++) {
total += data[c];
}
}



可能花费时间来处理溢出中断,
未初始化的数据[]提供了更多。也许你的编译器不够聪明,不能用d *循环替换总数* = 50000
(两次)。从源代码中并不总是清楚地看到测量的运行时间花费在哪里。



Maybe the time is spent servicing overflow interrupts, and the
uninitialised data[] gives more of them. Maybe your compiler
is not smart enough to replace the d-loop by total *= 50000
(both times). It is not always clear from the source code just
where the measured run time is being spent.




我测试了将数据和总变量更改为无符号它和

没关系。


另外,我尝试更改编译器参数


-O2 -Wall -D_LARGEFILE64_SOURCE -std = gnu99


这个也没有效果。


/ tom



I tested with changing the data and total variables to unsigned and it
did not matter.

Additionally, I tried changing the compiler arguments

-O2 -Wall -D_LARGEFILE64_SOURCE -std=gnu99

By that had no effect either.

/tom

这篇关于移除循环导致它以一半的速度移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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