2 Taken from samba/lib/talloc/testsuite.c: Unix SMB/CIFS implementation.
4 local testing of talloc routines.
6 Copyright (C) Andrew Tridgell 2004
8 ** NOTE! The following LGPL license applies to the talloc
9 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
25 #include <ccan/talloc/talloc.h>
26 #include <ccan/tal/tal.h>
27 #include <ccan/time/time.h>
28 #include <ccan/err/err.h>
33 int main(int argc, char *argv[])
39 void *p1, *p2[100], *p3[100];
40 bool run_talloc = true, run_tal = true, run_malloc = true;
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;
50 errx(1, "Bad flag %s", argv[1]);
56 ctx = talloc_new(NULL);
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);
68 count += (1 + 200) * LOOPS;
69 } while (time_sub(time_now(), tv).tv_sec < 5);
71 fprintf(stderr, "talloc: %.0f ops/sec\n", count/5.0);
79 ctx = tal(NULL, char);
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);
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);
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");
110 for (j = 0; j < 100; j++) {
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);
121 printf("success: speed\n");