2011/4/11 Дмитрий Оксенчук <oksenchuk89@xxxxxxxxx>: > > Yes, I run tests 3 times and this is average results. I used time > utility. Time for create and run this program without loop is 0.002s, > so this time is not significant. System busy was little and the same > in all tests. > Wouldn't it be better to use something like getrusage()? Like in the attached bit of silliness? kevin
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <limits.h> #include <sys/resource.h> volatile inline double m_pi_minus_fldpi(void) { double diff,pi=M_PI; asm( "fldpi\n\t" "fsubp\n" :"=t" (diff) :"0" (pi) ); return diff; } volatile inline float xa(const float a, const float b) { return (a*M_PI)+1.0/b; } volatile inline float xb(const float a, const float b) { float c; /* * (a*M_PI)+1.0/b */ asm( "fldpi\n\t" "fmulp\n\t" "fld1\n\t" "fdivp %%st,%%st(2)\n\t" "faddp" :"=t" (c) :"0" (a), "u" (b) :"st(1)" ); return c; } int main1(int argc, char *argv[]) { float a,b,res0,res1; if(argc<3) { fprintf(stderr,__FILE__"`%s()-%d: usage: %s clock a b\n", __func__,__LINE__,argv[0]); return -1; } a=(float)atof(argv[1]); b=(float)atof(argv[2]); printf("argv[1]=%s, argv[2]=%s\n",argv[1],argv[2]); res0=xa(a,b); res1=xb(a,b); printf("a=%f, b=%f, xa()=%f, xb()=%f\n",a,b,res0,res1); return 0; } #define LOOPS INT_MAX void printResults(struct rusage *after, struct rusage *before, int loops, int clock) { double loopsf,utime; long sec,usec; sec=after->ru_utime.tv_sec-before->ru_utime.tv_sec; usec=after->ru_utime.tv_usec-before->ru_utime.tv_usec; if(usec<0) { sec--; usec+=1000000; } printf("\t%d.%06du, %d.%06ds\n",sec,usec, after->ru_stime.tv_sec,after->ru_stime.tv_usec); loopsf=(double) loops; utime=sec+usec/1000000.0; printf("\t%f loops/sec\n",loopsf/utime); if(clock>0) printf("\t%f cycles/loop\n",clock/loopsf*utime); } int main(int argc, char *argv[]) { struct rusage ru_b,ru_a; int clock=0,argc0; volatile int m=1,n,o; volatile unsigned int l; volatile float c,d; float a,b; double diff; char *argv0[4]; a=1.0; b=2.2; argv0[0]=argv[0]; argv0[1]="1.0"; argv0[2]="2.2"; argv0[3]=NULL; argc0=3; if(argc>1) clock=atoi(argv[1]); if(argc>3) { a=(float)atof(argv[2]); b=(float)atof(argv[3]); argv0[1]=argv[2]; argv0[2]=argv[3]; } getrusage(RUSAGE_SELF,&ru_b); for(l=LOOPS;l;l--) ; getrusage(RUSAGE_SELF,&ru_a); printf("Empty loop:\n"); printResults(&ru_a,&ru_b,LOOPS,clock); getrusage(RUSAGE_SELF,&ru_b); for(l=LOOPS;l;l--) c=xa(a,b); getrusage(RUSAGE_SELF,&ru_a); printf("xa()=%g (straight c):\n",c); printResults(&ru_a,&ru_b,LOOPS,clock); getrusage(RUSAGE_SELF,&ru_b); for(l=LOOPS;l;l--) d=xb(a,b); getrusage(RUSAGE_SELF,&ru_a); printf("xb()=%g (embedded asm):\n",d); printResults(&ru_a,&ru_b,LOOPS,clock); getrusage(RUSAGE_SELF,&ru_b); n=0x100000; o=0xff; for(l=LOOPS;l;l--) m=n*o; getrusage(RUSAGE_SELF,&ru_a); printf("m = 0x100000 x 0xff:\n"); printResults(&ru_a,&ru_b,LOOPS,clock); getrusage(RUSAGE_SELF,&ru_b); for(l=LOOPS;l;l--) m=o*0; getrusage(RUSAGE_SELF,&ru_a); printf("m = 0xff x 0x100000:\n"); printResults(&ru_a,&ru_b,LOOPS,clock); (void) main1(argc0,argv0); diff=m_pi_minus_fldpi(); printf("m_pi_minus_fldpi()=%g\n",diff); return 0; }