Actual source code: PetscMalloc.c
petsc-3.7.3 2016-08-01
2: #include <petscsys.h>
3: #include <petsctime.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: PetscBool flg;
16: PetscInitialize(&argc,&argv,0,0);
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: }
27: /* Take care of paging effects */
28: PetscMalloc(100,&dummy);
29: PetscFree(dummy);
30: PetscTime(&x);
32: /* Do all mallocs */
33: for (i=0; i< 1000; i++) {
34: PetscMalloc(rand1[i],&arr[i]);
35: }
37: PetscTime(&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: PetscTime(&y);
50: for (i=0; i< 1000; i++) {
51: PetscFree(arr[i]);
52: }
54: fprintf(stdout,"%-15s : %e sec, with options : ","PetscMalloc",(y-x)/500.0);
55: PetscOptionsHasName(NULL,"-malloc",&flg);
56: if (flg) fprintf(stdout,"-malloc ");
57: fprintf(stdout,"\n");
59: PetscRandomDestroy(&r);
60: PetscFinalize();
61: return(0);
62: }