From 608caaf9b90786a16a8f6784ba0d126ef5ff84eb Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Fri, 14 Apr 2017 11:21:32 +0200 Subject: [PATCH] Add new benchmark. --- bench/Makefile | 12 +++++- bench/bench-plain-c.c | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 bench/bench-plain-c.c diff --git a/bench/Makefile b/bench/Makefile index 15c82845..621a77f3 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -4,7 +4,9 @@ CFLAGS=-O2 -march=native -Wall CPPFLAGS=-I.. N=4 -all: bench-mlib bench-mlib-mempool bench-stl bench-klib bench-qt bench-glib bench-libdynamic +all: benchmark bench-plain + +benchmark: bench-mlib bench-mlib-mempool bench-stl bench-klib bench-qt bench-glib bench-libdynamic bench-mlib: $(CC) $(CFLAGS) $(CPPFLAGS) bench-mlib.c -o bench-mlib.exe @@ -79,9 +81,17 @@ bench-libdynamic0: @./bench-libdynamic.exe 6 @./bench-libdynamic.exe 7 +bench-plain: + $(CC) $(CFLAGS) $(CPPFLAGS) bench-plain-c.c -o bench-plain-c.exe + @./bench-plain-c.exe + +############################################################################ + clean: rm -f *.o bench-*.exe bench-*.s *~ massif.out.* +############################################################################ + massif-do: all @valgrind --tool=massif ./bench-mlib.exe $(N) @valgrind --tool=massif ./bench-mlib-mempool.exe $(N) diff --git a/bench/bench-plain-c.c b/bench/bench-plain-c.c new file mode 100644 index 00000000..cbaefb5e --- /dev/null +++ b/bench/bench-plain-c.c @@ -0,0 +1,90 @@ +// Inspired by: http://stackoverflow.com/questions/3664272/is-stdvector-so-much-slower-than-plain-arrays/3664349#3664349 + +#define NDEBUG + +#include +#include +#include +#include +#include +#include + +#include "m-array.h" +#include "m-list.h" +#include "m-rbtree.h" +#include "m-dict.h" +#include "m-algo.h" +#include "m-mempool.h" +#include "m-string.h" + +unsigned long g_result; + +static unsigned long long +cputime (void) +{ + struct rusage rus; + getrusage (0, &rus); + return rus.ru_utime.tv_sec * 1000000ULL + rus.ru_utime.tv_usec; +} + +struct Pixel +{ + unsigned char r, g, b; +}; + +ARRAY_DEF(pixel, struct Pixel, (INIT(M_CLEAR_DEFAULT)) ) + +void UseMlib(void) +{ + unsigned long long t1 = cputime(); + + for(int i = 0; i < 1000; ++i) + { + int dimension = 999; + + array_pixel_t pixels; + array_pixel_init(pixels); + array_pixel_reserve(pixels, dimension * dimension); + + for(int i = 0; i < dimension * dimension; ++i) + { + const struct Pixel p = { 255, 0, 0}; + array_pixel_push_back (pixels, p); + } + + array_pixel_clear(pixels); + } + unsigned long long t2 = cputime(); + printf ("M*Lib Array: %Lu us\n", (t2-t1)/1000); +} + +void UseNative(void) +{ + unsigned long long t1 = cputime(); + + for(int i = 0; i < 1000; ++i) + { + int dimension = 999; + + struct Pixel * pixels = malloc(sizeof(struct Pixel) * dimension * dimension); + + for(int i = 0 ; i < dimension * dimension; ++i) + { + pixels[i].r = 255; + pixels[i].g = 0; + pixels[i].b = 0; + } + + free(pixels); + } + unsigned long long t2 = cputime(); + printf ("Native Array: %Lu us\n", (t2-t1)/1000); +} + +int main(void) +{ + UseNative(); + UseMlib(); + + return 0; +}