The strange speed of a program!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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?

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 


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) );
  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;



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux