1 #include <petscsys.h> 2 #include <petsctime.h> 3 4 int main(int argc,char **argv) 5 { 6 PetscLogDouble x,y; 7 double value; 8 void *arr[1000],*dummy; 9 int i,rand1[1000],rand2[1000]; 10 PetscRandom r; 11 PetscBool flg; 12 13 PetscCall(PetscInitialize(&argc,&argv,0,0)); 14 PetscCall(PetscRandomCreate(PETSC_COMM_SELF,&r)); 15 PetscCall(PetscRandomSetFromOptions(r)); 16 for (i=0; i<1000; i++) { 17 PetscCall(PetscRandomGetValue(r,&value)); 18 rand1[i] = (int)(value* 144327); 19 PetscCall(PetscRandomGetValue(r,&value)); 20 rand2[i] = (int)(value* 144327); 21 } 22 23 /* Take care of paging effects */ 24 PetscCall(PetscMalloc1(100,&dummy)); 25 PetscCall(PetscFree(dummy)); 26 PetscCall(PetscTime(&x)); 27 28 /* Do all mallocs */ 29 for (i=0; i< 1000; i++) PetscCall(PetscMalloc1(rand1[i],&arr[i])); 30 31 PetscCall(PetscTime(&x)); 32 33 /* Do some frees */ 34 for (i=0; i< 1000; i+=2) PetscCall(PetscFree(arr[i])); 35 36 /* Do some mallocs */ 37 for (i=0; i< 1000; i+=2) PetscCall(PetscMalloc1(rand2[i],&arr[i])); 38 PetscCall(PetscTime(&y)); 39 40 for (i=0; i< 1000; i++) PetscCall(PetscFree(arr[i])); 41 42 fprintf(stdout,"%-15s : %e sec, with options : ","PetscMalloc",(y-x)/500.0); 43 PetscCall(PetscOptionsHasName(NULL,"-malloc",&flg)); 44 if (flg) fprintf(stdout,"-malloc "); 45 fprintf(stdout,"\n"); 46 47 PetscCall(PetscRandomDestroy(&r)); 48 PetscCall(PetscFinalize()); 49 return 0; 50 } 51