Exemplu de masurare a duratei de executie


a unei secvente de cod folosind functia ftime din biblioteca sys\timeb.h (time0.c)
#include <stdio.h>
#include <sys\timeb.h>
#include <dos.h>
#include <conio.h>
/*
struct timeb {
   long time ;//timpul in secunde de la 01/01/1970
   short millitm ;//milisecunde
   short timezone ;//dif dintre timpul local si GMT in
   short dstflag ;//0 daca nu este orar de vara
 };
sintaxa ftine este

void ftime(struct timeb *buf);
*/
timeb start, end;long dif;double flops;
#define NROP 900000
main(){
clrscr();
int out=0;char ch;float a=3.21, b=1.23;
while(out==0){
  if(kbhit()!=0){ch=getch();if(ch==27) out=1;};
  ftime(&start);
  //delay(1000);
  for(long i=0;i<NROP;i++){
  a=(a-b)+(a-b)+(a-b)+(a-b)+(a-b)+(b-a)+(b-a)+(b-a)+(b-a)+b+a-a;//20 oper + sau -
  a=(a/b)*(a/b)*(a/b)*(a/b)*(a/b)*(b/a)*(b/a)*(b/a)*(b/a)*b*a/a;//20 oper * sau /
  }
  ftime(&end);
  dif=(end.time-start.time)*1000+end.millitm-start.millitm;//dif in milisec
  gotoxy(1,1);clreol();gotoxy(1,1);
  printf("dif=%d",dif);printf(" a=%E\n",a);
  gotoxy(1,2);clreol();gotoxy(1,2);
  flops=NROP*40/(dif/1000.0)/1e6;
  printf("Nr oper in MFLOPS=%f",flops);
  }
}

Cornel Mironel Niculae, 2003-2004

13-Jan-2004