This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Simple malloc benchtest.


Hi,

Here is expansion a test that I wrote earlier for measuring malloc
running time.

It should be used mostly for microoptimizations that does not change how
allocations are made, this rules out effect for other factors.


	* benchtests/bench-malloc-small.c: New file.
	* benchtests/Makefile: Add malloc-small

---
 benchtests/Makefile             |    6 +++-
 benchtests/bench-malloc-small.c |   69 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 benchtests/bench-malloc-small.c

diff --git a/benchtests/Makefile b/benchtests/Makefile
index 117228b..430a708 100644
--- a/benchtests/Makefile
+++ b/benchtests/Makefile
@@ -31,9 +31,13 @@ string-bench := bcopy bzero memccpy memchr memcmp memcpy memmem memmove \
 		strspn strstr strcpy_chk stpcpy_chk memrchr strsep strtok
 string-bench-all := $(string-bench)
 
+malloc-bench := malloc-small
+
 stdlib-bench := strtod
 
-benchset := $(string-bench-all) $(stdlib-bench)
+
+
+benchset := $(malloc-bench) $(string-bench-all) $(stdlib-bench)
 
 LDLIBS-bench-acos = -lm
 LDLIBS-bench-acosh = -lm
diff --git a/benchtests/bench-malloc-small.c b/benchtests/bench-malloc-small.c
new file mode 100644
index 0000000..a62a9f6
--- /dev/null
+++ b/benchtests/bench-malloc-small.c
@@ -0,0 +1,69 @@
+/* Measure malloc and free running time.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#define TEST_MAIN
+#define TEST_NAME "malloc"
+#include "bench-string.h"
+
+void
+do_test (size_t size)
+{
+  timing_t start, stop, cur;
+  const int iters = 1<<18;
+
+  unsigned int r_seed = 42;
+  void *ary[iters];
+  size_t sizes[iters];
+  size_t idx[iters];
+
+  for (int i = 0; i < iters; i++)
+     {
+       ary[i] = NULL;
+       sizes[i] = size + rand_r (&r_seed) % (7 * size);
+       idx[i] = rand_r (&r_seed) % (iters / 64);
+     }
+
+  printf ("\n allocations in range %lu-%lu:", size, 8 * size);
+
+  TIMING_NOW (start);
+
+  for (int i = 0; i < iters; ++i)
+    {
+      free (ary[idx[i]]);
+      ary[idx[i]] = malloc (sizes[i]);
+    }
+
+  for (int i = 0; i < iters; ++i)
+    free (ary[i]);
+
+  TIMING_NOW (stop);
+  TIMING_DIFF (cur, start, stop);
+  TIMING_PRINT_MEAN ((double) cur, (double) iters);
+}
+
+static int
+test_main (void)
+{
+  for (int j=0; j < 5; ++j)
+    for (int i = 1; i < 5000; i *= 2)
+      do_test (i);
+
+  return 0;
+}
+
+#include "../test-skeleton.c"
-- 
1.7.10.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]