Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LALMallocPerf.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Karl Wette
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20/**
21 * \file
22 * \ingroup LALMalloc_h
23 * \brief Tests the performance of the routines in \ref LALMalloc_h.
24 */
25
26/** \cond DONT_DOXYGEN */
27
28#include <stdio.h>
29
30#include <gsl/gsl_rng.h>
31#include <gsl/gsl_randist.h>
32
33#include <lal/LALStdlib.h>
34#include <lal/LALMalloc.h>
35#include <lal/LogPrintf.h>
36
37int main(void) {
38
39 setvbuf(stdout, NULL, _IONBF, 0);
40
41 printf("LALMallocPerf: LAL memory tracking is %s\n", (lalDebugLevel & LALMEMTRKBIT) ? "ON" : "OFF");
42
43 const int n = 1 << 17;
44 printf("LALMallocPerf: Testing performance with %i allocates\n", n);
45
46 {
47 void *x[n];
48 for (int i = 0; i < n; ++i) {
49 x[i] = XLALMalloc(sizeof(int));
50 }
51 printf("LALMallocPerf: Deallocate in same order as allocate:\t");
52 const REAL8 t0 = XLALGetCPUTime();
53 for (int i = 0; i < n; ++i) {
54 XLALFree(x[i]);
55 }
56 const REAL8 t = XLALGetCPUTime() - t0;
57 printf("%g sec (%e sec/deallocate)\n", t, t/n);
58 }
59
60 {
61 void *x[n];
62 for (int i = 0; i < n; ++i) {
63 x[i] = XLALMalloc(sizeof(int));
64 }
65 printf("LALMallocPerf: Deallocate in reverse order as allocate:\t");
66 const REAL8 t0 = XLALGetCPUTime();
67 for (int i = n - 1; i >= 0; --i) {
68 XLALFree(x[i]);
69 }
70 const REAL8 t = XLALGetCPUTime() - t0;
71 printf("%g sec (%e sec/deallocate)\n", t, t/n);
72 }
73
74 {
75 void *x[n];
76 int ii[n];
77 for (int i = 0; i < n; ++i) {
78 x[i] = XLALMalloc(sizeof(int));
79 ii[i] = i;
80 }
81 gsl_rng_env_setup();
82 gsl_rng *r = gsl_rng_alloc(gsl_rng_default);
83 gsl_ran_shuffle(r, ii, n, sizeof(int));
84 gsl_rng_free(r);
85 printf("LALMallocPerf: Deallocate in random order:\t\t");
86 const REAL8 t0 = XLALGetCPUTime();
87 for (int i = 0; i < n; ++i) {
88 XLALFree(x[ii[i]]);
89 }
90 const REAL8 t = XLALGetCPUTime() - t0;
91 printf("%g sec (%e sec/deallocate)\n", t, t/n);
92 }
93
95
96 return EXIT_SUCCESS;
97
98}
99
100/** \endcond */
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
int main(int argc, char *argv[])
Definition: cache.c:25
double REAL8
Double precision real floating-point number (8 bytes).
#define lalDebugLevel
Definition: LALDebugLevel.h:58
@ LALMEMTRKBIT
enable memory tracking
Definition: LALDebugLevel.h:39
#define XLALMalloc(n)
Definition: LALMalloc.h:44
#define XLALFree(p)
Definition: LALMalloc.h:47
REAL8 XLALGetCPUTime(void)
High-resolution CPU timer (returns result in seconds), aimed for code-timing purposes.
Definition: LogPrintf.c:270
static const INT4 r
Definition: Random.c:82