]>
git.ipfire.org Git - people/ms/strongswan.git/blob - scripts/malloc_speed.c
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec aG
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include <utils/debug.h>
23 #endif /* HAVE_MALLINFO */
25 static void start_timing(struct timespec
*start
)
27 clock_gettime(CLOCK_THREAD_CPUTIME_ID
, start
);
30 static double end_timing(struct timespec
*start
)
34 clock_gettime(CLOCK_THREAD_CPUTIME_ID
, &end
);
35 return (end
.tv_nsec
- start
->tv_nsec
) / 1000000000.0 +
36 (end
.tv_sec
- start
->tv_sec
) * 1.0;
39 static void print_mallinfo()
42 struct mallinfo mi
= mallinfo();
44 printf("malloc: sbrk %d, mmap %d, used %d, free %d\n",
45 mi
.arena
, mi
.hblkhd
, mi
.uordblks
, mi
.fordblks
);
46 #endif /* HAVE_MALLINFO */
52 int main(int argc
, char *argv
[])
54 struct timespec timing
;
57 /* a random set of allocations we test */
58 int sizes
[16] = { 1, 13, 100, 1000, 16, 10000, 50, 17,
59 123, 32, 8, 64, 8096, 1024, 123, 9 };
61 library_init(NULL
, "malloc_speed");
62 atexit(library_deinit
);
66 start_timing(&timing
);
68 for (round
= 0; round
< ROUNDS
; round
++)
70 for (i
= 0; i
< ALLOCS
; i
++)
72 m
[i
] = malloc(sizes
[(round
+ i
) % countof(sizes
)]);
74 for (i
= 0; i
< ALLOCS
; i
++)
79 printf("time for %d malloc/frees, repeating %d rounds: %.4fs\n",
80 ALLOCS
, ROUNDS
, end_timing(&timing
));