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/tal/str/str.h>
28 #include <ccan/time/time.h>
29 #include <ccan/err/err.h>
34 int main(int argc, char *argv[])
40 void *p1, *p2[100], *p3[100];
41 bool run_talloc = true, run_tal = true, run_malloc = true;
44 if (strcmp(argv[1], "--talloc") == 0)
45 run_tal = run_malloc = false;
46 else if (strcmp(argv[1], "--tal") == 0)
47 run_talloc = run_malloc = false;
48 else if (strcmp(argv[1], "--malloc") == 0)
49 run_talloc = run_tal = false;
51 errx(1, "Bad flag %s", argv[1]);
57 ctx = talloc_new(NULL);
61 for (i=0;i<LOOPS;i++) {
62 p1 = talloc_size(ctx, LOOPS % 128);
63 for (j = 0; j < 100; j++) {
64 p2[j] = talloc_strdup(p1, "foo bar");
65 p3[j] = talloc_size(p1, 300);
69 count += (1 + 200) * LOOPS;
70 } while (time_sub(time_now(), tv).tv_sec < 5);
72 fprintf(stderr, "talloc: %.0f ops/sec\n", count/5.0);
80 ctx = tal(NULL, char);
84 for (i=0;i<LOOPS;i++) {
85 p1 = tal_arr(ctx, char, LOOPS % 128);
86 for (j = 0; j < 100; j++) {
87 p2[j] = tal_strdup(p1, "foo bar");
88 p3[j] = tal_arr(p1, char, 300);
92 count += (1 + 200) * LOOPS;
93 } while (time_sub(time_now(), tv).tv_sec < 5);
94 fprintf(stderr, "tal: %.0f ops/sec\n", count/5.0);
105 for (i=0;i<LOOPS;i++) {
106 p1 = malloc(LOOPS % 128);
107 for (j = 0; j < 100; j++) {
108 p2[j] = strdup("foo bar");
111 for (j = 0; j < 100; j++) {
117 count += (1 + 200) * LOOPS;
118 } while (time_sub(time_now(), tv).tv_sec < 5);
119 fprintf(stderr, "malloc: %.0f ops/sec\n", count/5.0);
122 printf("success: speed\n");