]> git.ozlabs.org Git - ccan/blob - ccan/tal/benchmark/speed.c
tal: simplify.
[ccan] / ccan / tal / benchmark / speed.c
1 /*
2    Taken from samba/lib/talloc/testsuite.c: Unix SMB/CIFS implementation.
3
4    local testing of talloc routines.
5
6    Copyright (C) Andrew Tridgell 2004
7
8      ** NOTE! The following LGPL license applies to the talloc
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 */
25 #include <ccan/talloc/talloc.h>
26 #include <ccan/tal/tal.h>
27 #include <ccan/time/time.h>
28 #include <ccan/err/err.h>
29 #include <string.h>
30
31 #define LOOPS 1024
32
33 int main(int argc, char *argv[])
34 {
35         void *ctx;
36         unsigned count;
37         int i, j;
38         struct timespec tv;
39         void *p1, *p2[100], *p3[100];
40         bool run_talloc = true, run_tal = true, run_malloc = true;
41
42         if (argv[1]) {
43                 if (strcmp(argv[1], "--talloc") == 0)
44                         run_tal = run_malloc = false;
45                 else if (strcmp(argv[1], "--tal") == 0)
46                         run_talloc = run_malloc = false;
47                 else if (strcmp(argv[1], "--malloc") == 0)
48                         run_talloc = run_tal = false;
49                 else
50                         errx(1, "Bad flag %s", argv[1]);
51         }
52
53         if (!run_talloc)
54                 goto after_talloc;
55
56         ctx = talloc_new(NULL);
57         tv = time_now();
58         count = 0;
59         do {
60                 for (i=0;i<LOOPS;i++) {
61                         p1 = talloc_size(ctx, LOOPS % 128);
62                         for (j = 0; j < 100; j++) {
63                                 p2[j] = talloc_strdup(p1, "foo bar");
64                                 p3[j] = talloc_size(p1, 300);
65                         }
66                         talloc_free(p1);
67                 }
68                 count += (1 + 200) * LOOPS;
69         } while (time_sub(time_now(), tv).tv_sec < 5);
70
71         fprintf(stderr, "talloc: %.0f ops/sec\n", count/5.0);
72
73         talloc_free(ctx);
74
75 after_talloc:
76         if (!run_tal)
77                 goto after_tal;
78
79         ctx = tal(NULL, char);
80         tv = time_now();
81         count = 0;
82         do {
83                 for (i=0;i<LOOPS;i++) {
84                         p1 = tal_arr(ctx, char, LOOPS % 128);
85                         for (j = 0; j < 100; j++) {
86                                 p2[j] = tal_strdup(p1, "foo bar");
87                                 p3[j] = tal_arr(p1, char, 300);
88                         }
89                         tal_free(p1);
90                 }
91                 count += (1 + 200) * LOOPS;
92         } while (time_sub(time_now(), tv).tv_sec < 5);
93         fprintf(stderr, "tal:    %.0f ops/sec\n", count/5.0);
94
95         tal_free(ctx);
96
97 after_tal:
98         if (!run_malloc)
99                 goto after_malloc;
100
101         tv = time_now();
102         count = 0;
103         do {
104                 for (i=0;i<LOOPS;i++) {
105                         p1 = malloc(LOOPS % 128);
106                         for (j = 0; j < 100; j++) {
107                                 p2[j] = strdup("foo bar");
108                                 p3[j] = malloc(300);
109                         }
110                         for (j = 0; j < 100; j++) {
111                                 free(p2[j]);
112                                 free(p3[j]);
113                         }
114                         free(p1);
115                 }
116                 count += (1 + 200) * LOOPS;
117         } while (time_sub(time_now(), tv).tv_sec < 5);
118         fprintf(stderr, "malloc: %.0f ops/sec\n", count/5.0);
119
120 after_malloc:
121         printf("success: speed\n");
122
123         return 0;
124 }