程序的奇怪速度! [英] The strange speed of a program!

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

问题描述

大家好,

我有一个程序,其速度对我来说太奇怪了。它主要用于

来计算输出图像所以从四个图像s0,s1,s2,s3中得到

so =(s0- s2)^ 2 +(s1-s3)^ 2。我用gcc编译它(没有优化)。 / *********** /之间的
编解码器是初始化代码。是什么让我很开心

是初始化的代码(io == 1)比没有初始化的代码快得多(io!= 1)。初始化代码需要一些时间

,它应该会减慢程序的速度。但结果让我感到困惑。为什么?


代码列表如下

======================== ========================== ================

#include< stdio.h>

#include< time.h>

#include< stdlib.h>

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

{

clock_t start,end;

double cpu_time_used;

short * s0,* s1,* s2,* s3;

int * so;

unsigned int i,j;

unsigned int时间;

unsigned int length;

int io;

if(argc< 4)

{

fprintf(stderr," USAGE:%s乘以宽度(1 initialize,otherwize

noinitialize)\ n",argv [0]);

退出(1);

}

其他

{

times = atoi(argv [1] );

length = atoi(argv [2]);

length = length * length;

io = atoi(argv [3] );

}

s0 =(短*)malloc(长度* sizeof(短));

s1 =(短*)malloc (长* s izeof(短));

s2 =(短*)malloc(长* sizeof(短));

s3 =(短*)malloc(长* sizeof(简短));

s3 =(短*)malloc(长度* sizeof(短));

so =(int *)malloc(length * sizeof(int) );

start = clock();

for(i = 0; I<倍; ++ i)

{

/ **************************** ********************** /

if(io == 1)

{

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

{

s0 [j] = i + j;

s1 [j] = length-1-j;

s2 [j] = 2 * j;

s3 [j] = 3 * j;

}

}

/ ************************* ************************* /

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

{

int tmp1,tmp2;

tmp1 = s0 [j] -s2 [j];

tmp1 = tmp1 * tmp1;

tmp2 = s1 [j] -s3 [j];

tmp2 = tmp2 * tmp2;

所以[j] = tmp1 + tmp2;

}

}

end = clock();

cpu_time_used =((double )(结束 - 开始))/ CLOCKS_PER_SEC;

printf(CPU时间:%f sec\ n,cpu_time_used);

free(s0);

免费(s1);

免费(s2);

免费(s3);

免费(所以) ;

返回0;

}

解决方案

be ar写道:

大家好,
我有一个程序,速度对我来说很奇怪。让我很多的是初始化代码(io == 1)比没有初始化(io!= 1)要快得多。初始化代码应该花费一些时间,它应该减慢程序。
但结果让我感到困惑。为什么?


你能发布你观察到的实际时间吗?

唯一想到的是,

中的操作io == 1版本都是小数字,所以也许他们不会只要io == 0版本,这些都是随机数字

这可能是大的和/或负面的。此外,大量的

操作可能会导致整数溢出异常。

#include< stdio.h>
#include< time.h>
#include< stdlib.h>
int main(int argc,char * argv [])
{
clock_t start,end;
double cpu_time_used;
short * s0,* s1,* s2,* s3;
int * so;
unsigned int i,j;
unsigned int times;
unsigned int length;
int io;
if(argc< 4)
{/ fprintf(stderr," USAGE:%s乘以宽度(1 initialize,otherwize
noinitialize)\ n,argv [0]);
exit(1);
}

{
times = atoi(argv [1]);
length = atoi(argv [2]);


atoi会导致未定义的行为,如果数字大于

可以适合int

length = length * length;


您应该检查长度*长度是否会溢出,然后

这样做。

io = atoi(argv [3] ]);
}
s0 =(短*)malloc(长度* sizeof(短));


失去演职员表(搜索此新闻组的''malloc''学习

更多关于原因):


s0 = malloc(length * sizeof * s0);

s1 =(短*)malloc(长* sizeof(短));
s2 =(短*)malloc(长度* sizeof(短));
s3 =(短*)malloc(长* sizeof(短));
s3 =(短*)malloc(长* sizeof(短));


你刚刚泄露了第一个s3 malloc的结果。

你还应该检查所有这些mallocs是否成功。

so =(int *)malloc(length * sizeof(int));




其余代码看起来还不错。


感谢您的建议。


我使用gcc-3.3.5编译basic.c而没有优化。我的CPU是

athlon-xp,安装了Debian。


../basic 50 1024 0 #no init

结果CPU时间:9.690000秒

../basic 50 1024 1 #init

导致CPU时间:3.280000秒

Old Wolf写道:< blockquote class =post_quotes> bear写道:

大家好,
我有一个程序,速度对我来说太奇怪了。让我很多的是初始化代码(io == 1)比没有初始化(io!= 1)要快得多。初始化代码应该花费一些时间,它应该减慢程序。
但结果让我感到困惑。为什么?
你能发布你观察到的实际时间吗?
唯一想到的是,
io == 1版本中的操作都是小数字,所以也许他们只要io == 0版本,这些都是随机数
可能很大和/或负面。此外,大量的操作可能会导致整数溢出异常。




随机数的计算运行缓慢而不是小数?

我认为它们使用相同的指令和相同的操作数宽度。

此外,这些溢出异常是否会导致速度变慢?

#include< stdio.h>
#include< time.h>
#include< stdlib.h>
int main(int argc,char * argv [])
{
clock_t start,end;
double cpu_time_used;
short * s0,* s1,* s2,* s3;
int * so;
unsigned int i,j;
unsigned int times;
unsigned int length;
int io;
if(argc< 4)
{
fprintf(stderr," USAGE: %s乘以宽度(1初始化,其他打算
无初始化)\ n",argv [0]);
退出(1);
}

{
times = atoi(argv [1]);
length = atoi(argv [2]);
atoi原因未定义的行为如果数字大于
可以适合int

length = length * length;



你应该检查在
这样做之前,长度*长度不会溢出。

io = atoi(argv [3]);
}
s0 =(短*)malloc(长度* sizeof(短));



失去演员阵容(搜索此新闻组的''malloc''以了解更多关于原因的内容):




我搜索但没有找到任何关于为什么这个演员表不正确的线索。

所以我就把它留在那里。

s0 = malloc(length * sizeof * s0);

s1 =(short *)malloc(length * sizeof(short));
s2 =(short *)malloc(length * sizeof(short));
s3 =(short *)malloc(length * sizeof(short));
s3 =(short *)malloc(length * sizeof(short)) );



你刚刚泄露了第一个s3 malloc的结果s。
你还应检查所有这些mallocs是否成功。



我已经纠正了这个

< blockquote class =post_quotes> so =(int *)malloc(length * sizeof(int));



其余代码看起来没问题。




bear写道:

老狼写道:

bear写道:

。 ... snip ...

io = atoi(argv [3]);
}
s0 =(short *)malloc(length * sizeof(short));



失去演员阵容(搜索这个新闻组''malloc''学习更多关于原因的信息):



我搜索但是没有找到关于为什么这个演员阵容不正确的任何线索。
所以我把它留在那里。




一般来说所有演员表,除了参数variadic

函数,是可疑的,应该避免。不必要的演员

只能通过宣布我知道我正在做什么,所以不要抱怨来抑制错误信息。 malloc调用的通常形式是:


if(!(ptr = malloc(number * sizeof * ptr))){

/ *处理内存不足条件* /

}

其他{

/ *成功分配,用ptr做任何事情* /

}


为任何类型的数字项数组保证空间

ptr已被声明指向。

ptr和number的声明和值(后者可能为1,因而省略)控制所有

操作,并且不会抑制任何错误消息。一个非常常见的

错误可以通过愚蠢的演员来抑制#include

< stdlib.h> ;.

请务必阅读以下前三个参考文献。


-

关于C的一些有用的参考:

< http://www.ungerhu.com/jxh/clc.welcome.txt>

< http://www.eskimo.com/~scs/C-faq/top.html>

< http://benpfaff.org/writings/clc/off-topic.html>

< http://anubis.dkuug.dk/jtc1/sc22 / WG14 /网络/文档/ n869 /> (C99)

< http://www.dinkumware.com/refxc.html> (C库}

< http://gcc.gnu.org/onlinedocs/>(GNU docs)


hi all,
I have a program whose speed is so strange to me. It is maily used
to
calculate a output image so from four images s0,s1,s2,s3 where
so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no optimization). the
codec between /***********/ is the initialization code. What supprise
me a lot
is the code with initialization(io==1) is much faster than without
initialization(io!=1). The initialization code should takes some time
and it should slow down the program. But the result confuse me. Why?

Code is listed below
================================================== ================
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
clock_t start,end;
double cpu_time_used;
short *s0,*s1,*s2,*s3;
int *so;
unsigned int i,j;
unsigned int times;
unsigned int length;
int io;
if( argc<4 )
{
fprintf( stderr,"USAGE: %s times width (1 initialize, otherwize
noinitialize)\n",argv[0] );
exit(1);
}
else
{
times = atoi( argv[1] );
length = atoi( argv[2] );
length = length*length;
io = atoi( argv[3] );
}
s0 = (short *)malloc( length*sizeof(short) );
s1 = (short *)malloc( length*sizeof(short) );
s2 = (short *)malloc( length*sizeof(short) );
s3 = (short *)malloc( length*sizeof(short) );
s3 = (short *)malloc( length*sizeof(short) );
so = (int *)malloc( length*sizeof(int) );
start = clock();
for( i=0; i<times; ++i)
{
/**************************************************/
if( io==1 )
{
for( j=0; j<length; ++j )
{
s0[j] = i+j;
s1[j] = length-1-j;
s2[j] = 2*j;
s3[j] = 3*j;
}
}
/**************************************************/
for( j=0; j<length; ++j )
{
int tmp1,tmp2;
tmp1 = s0[j]-s2[j];
tmp1 = tmp1*tmp1;
tmp2 = s1[j]-s3[j];
tmp2 = tmp2*tmp2;
so[j] = tmp1+tmp2;
}
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("CPU time: %f sec\n",cpu_time_used);
free( s0 );
free( s1 );
free( s2 );
free( s3 );
free( so );
return 0;
}

解决方案

bear wrote:

hi all,
I have a program whose speed is so strange to me. What supprise
me a lot is the code with initialization(io==1) is much faster
than without initialization(io!=1). The initialization code should
takes some time and it should slow down the program.
But the result confuse me. Why?
Can you post the actual timings you observed?
The only thing that comes to mind is, the operations in the
io==1 version are all on small numbers, so maybe they don''t
take as long as the io==0 version which are all on random numbers
which could be large and/or negative. Also, the large number
operations might be causing integer overflow exceptions.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
clock_t start,end;
double cpu_time_used;
short *s0,*s1,*s2,*s3;
int *so;
unsigned int i,j;
unsigned int times;
unsigned int length;
int io;
if( argc<4 )
{
fprintf( stderr,"USAGE: %s times width (1 initialize, otherwize
noinitialize)\n",argv[0] );
exit(1);
}
else
{
times = atoi( argv[1] );
length = atoi( argv[2] );
atoi causes undefined behaviour if the number is bigger than
can fit in an int
length = length*length;
You should check that length * length will not overflow, before
doing this.
io = atoi( argv[3] );
}
s0 = (short *)malloc( length*sizeof(short) );
Lose the cast (search this newsgroup for ''malloc'' to learn
more about why):

s0 = malloc( length * sizeof *s0 );
s1 = (short *)malloc( length*sizeof(short) );
s2 = (short *)malloc( length*sizeof(short) );
s3 = (short *)malloc( length*sizeof(short) );
s3 = (short *)malloc( length*sizeof(short) );
You just leaked the first s3 malloc''s results.
You should also check that all of these mallocs succeeded.
so = (int *)malloc( length*sizeof(int) );



Rest of the code looks OK.


Thanks for your advice.

I compile basic.c using gcc-3.3.5 with no optimization. My CPU is
athlon-xp,installed Debian.

../basic 50 1024 0 #no init
results in CPU time: 9.690000 sec
../basic 50 1024 1 #init
results in CPU time: 3.280000 sec
Old Wolf wrote:

bear wrote:

hi all,
I have a program whose speed is so strange to me. What supprise
me a lot is the code with initialization(io==1) is much faster
than without initialization(io!=1). The initialization code should
takes some time and it should slow down the program.
But the result confuse me. Why?
Can you post the actual timings you observed?
The only thing that comes to mind is, the operations in the
io==1 version are all on small numbers, so maybe they don''t
take as long as the io==0 version which are all on random numbers
which could be large and/or negative. Also, the large number
operations might be causing integer overflow exceptions.



The calculation of random number runs slowly than small numbers?
I think they use the same instruction and the same width of operand.
Also, can these overflow exceptions cause slow down?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
clock_t start,end;
double cpu_time_used;
short *s0,*s1,*s2,*s3;
int *so;
unsigned int i,j;
unsigned int times;
unsigned int length;
int io;
if( argc<4 )
{
fprintf( stderr,"USAGE: %s times width (1 initialize, otherwize
noinitialize)\n",argv[0] );
exit(1);
}
else
{
times = atoi( argv[1] );
length = atoi( argv[2] );
atoi causes undefined behaviour if the number is bigger than
can fit in an int

length = length*length;



You should check that length * length will not overflow, before
doing this.

io = atoi( argv[3] );
}
s0 = (short *)malloc( length*sizeof(short) );



Lose the cast (search this newsgroup for ''malloc'' to learn
more about why):



I search but do not find any clue on why this cast is incorrect.
So I just leave it there.

s0 = malloc( length * sizeof *s0 );

s1 = (short *)malloc( length*sizeof(short) );
s2 = (short *)malloc( length*sizeof(short) );
s3 = (short *)malloc( length*sizeof(short) );
s3 = (short *)malloc( length*sizeof(short) );



You just leaked the first s3 malloc''s results.
You should also check that all of these mallocs succeeded.


I have corrected this

so = (int *)malloc( length*sizeof(int) );



Rest of the code looks OK.




bear wrote:

Old Wolf wrote:

bear wrote:
.... snip ...

io = atoi( argv[3] );
}
s0 = (short *)malloc( length*sizeof(short) );



Lose the cast (search this newsgroup for ''malloc'' to learn
more about why):



I search but do not find any clue on why this cast is incorrect.
So I just leave it there.



In general ALL casts, other than to arguments to variadic
functions, are suspect and should be avoided. Unnecessary casts
only serve to suppress error messages, by announcing "I know what I
am doing, so don''t complain". The usual form for a malloc call is:

if (!(ptr = malloc(number * sizeof *ptr))) {
/* handle the "out of memory" condition */
}
else {
/* successful allocation, do whatever with ptr */
}

which secures space for an array of number items of whatever type
ptr has been declared to point to. The declarations and values of
ptr and number (the latter may be 1, and thus omitted) control all
the action, and no error messages are suppressed. A very common
error that can be suppressed by a silly cast is failure to #include
<stdlib.h>.

Make sure you read the first three references below.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)


这篇关于程序的奇怪速度!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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