Actual source code: PetscMalloc.c
petsc-3.14.6 2021-03-30
2: #include <petscsys.h>
3: #include <petsctime.h>
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];
12: PetscRandom r;
13: PetscBool flg;
15: PetscInitialize(&argc,&argv,0,0);if (ierr) return ierr;
16: PetscRandomCreate(PETSC_COMM_SELF,&r);
17: PetscRandomSetFromOptions(r);
18: for (i=0; i<1000; i++) {
19: PetscRandomGetValue(r,&value);
20: rand1[i] = (int)(value* 144327);
21: PetscRandomGetValue(r,&value);
22: rand2[i] = (int)(value* 144327);
23: }
25: /* Take care of paging effects */
26: PetscMalloc1(100,&dummy);
27: PetscFree(dummy);
28: PetscTime(&x);
30: /* Do all mallocs */
31: for (i=0; i< 1000; i++) {
32: PetscMalloc1(rand1[i],&arr[i]);
33: }
35: PetscTime(&x);
37: /* Do some frees */
38: for (i=0; i< 1000; i+=2) {
39: PetscFree(arr[i]);
40: }
42: /* Do some mallocs */
43: for (i=0; i< 1000; i+=2) {
44: PetscMalloc1(rand2[i],&arr[i]);
45: }
46: PetscTime(&y);
48: for (i=0; i< 1000; i++) {
49: PetscFree(arr[i]);
50: }
52: fprintf(stdout,"%-15s : %e sec, with options : ","PetscMalloc",(y-x)/500.0);
53: PetscOptionsHasName(NULL,"-malloc",&flg);
54: if (flg) fprintf(stdout,"-malloc ");
55: fprintf(stdout,"\n");
57: PetscRandomDestroy(&r);
58: PetscFinalize();
59: return ierr;
60: }