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,RANDOM_DEFAULT,&r);
     19:   for (i=0; i<1000; i++) {
     20:     PetscRandomGetValue(r,&value);
     21:     rand1[i] = (int)(value* 144327);
     22:     PetscRandomGetValue(r,&value);
     23:     rand2[i] = (int)(value* 144327);
     24:   }
     25:
     26:   /* Take care of paging effects */
     27:   PetscMalloc(100,&dummy);
     28:   PetscFree(dummy);
     29:   PetscGetTime(&x);

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

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

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