Actual source code: PetscMalloc.c

 2:  #include petsc.h
 3:  #include petscsys.h

  7: int main(int argc,char **argv)
  8: {
  9:   PetscLogDouble  x,y;
 10:   double      value;
 11:   void        *arr[1000],*dummy;
 12:   int         ierr,i,rand1[1000],rand2[1000];
 13:   PetscRandom r;
 14:   PetscTruth  flg;
 15: 
 16:   PetscInitialize(&argc,&argv,0,0);
 17: 
 18:   PetscRandomCreate(PETSC_COMM_SELF,&r);
 19:   PetscRandomSetFromOptions(r);
 20:   for (i=0; i<1000; i++) {
 21:     PetscRandomGetValue(r,&value);
 22:     rand1[i] = (int)(value* 144327);
 23:     PetscRandomGetValue(r,&value);
 24:     rand2[i] = (int)(value* 144327);
 25:   }
 26: 
 27:   /* Take care of paging effects */
 28:   PetscMalloc(100,&dummy);
 29:   PetscFree(dummy);
 30:   PetscGetTime(&x);

 32:   /* Do all mallocs */
 33:   for (i=0 ; i< 1000; i++) {
 34:     PetscMalloc(rand1[i],& arr[i]);
 35:   }
 36: 
 37:   PetscGetTime(&x);

 39:   /* Do some frees */
 40:   for (i=0; i< 1000; i+=2) {
 41:     PetscFree(arr[i]);
 42:   }

 44:   /* Do some mallocs */
 45:   for (i=0; i< 1000; i+=2) {
 46:     PetscMalloc(rand2[i],&arr[i]);
 47:  }
 48:   PetscGetTime(&y);
 49: 
 50:   for (i=0; i< 1000; i++) {
 51:     PetscFree(arr[i]);
 52:   }
 53: 
 54:   fprintf(stdout,"%-15s : %e sec, with options : ","PetscMalloc",(y-x)/500.0);
 55:   if(PetscOptionsHasName(PETSC_NULL,"-malloc",&flg),flg) fprintf(stdout,"-malloc ");
 56:   fprintf(stdout,"\n");
 57: 
 58:   PetscRandomDestroy(r);
 59:   PetscFinalize();
 60:   return(0);
 61: }