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