Skip to content

Commit

Permalink
Refactor map benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikwidlund committed Jan 22, 2017
1 parent da20a08 commit 1982366
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 109 deletions.
4 changes: 2 additions & 2 deletions benchmark/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ vector_CXXFLAGS = -I../src -g -O3 -flto -fuse-linker-plugin
vector_LDADD = ../libdynamic.la

map_SOURCES = \
map.c map_std_unordered.cpp map_custom.c map_dynamic.c \
map.h map_std_unordered.h map_custom.h map_dynamic.h \
map.c map_std_unordered.cpp map_custom.c map_libdynamic.c map_libdynamic_subclass.c \
map.h map_std_unordered.h map_custom.h map_libdynamic.h map_libdynamic_subclass.h \
map.R
#map.c map_custom.c map_subclass.c map_dynamic.c map_unordered.cpp \
#map.h map_custom.h map_subclass.h map_dynamic.h map_unordered.h \
Expand Down
11 changes: 5 additions & 6 deletions benchmark/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
#include <time.h>
#include "dynamic.h"

//#include "map.h"
//#include "map_subclass.h"
#include "map_std_unordered.h"
#include "map_custom.h"
//#include "map_dynamic.h"
#include "map_libdynamic.h"
#include "map_libdynamic_subclass.h"

typedef struct library library;
struct library
Expand Down Expand Up @@ -43,7 +42,7 @@ static library libraries[] = {
{.name = "std::map_unordered", .measure = map_std_unordered},
{.name = "custom open addressing", .measure = map_custom},
{.name = "libdynamic", .measure = map_libdynamic},
//{.name = "libdynamic map (subclass)", .measure = map_libdynamic_subclass}
{.name = "libdynamic (subclass)", .measure = map_libdynamic_subclass}
};
static const size_t libraries_len = sizeof libraries / sizeof libraries[0];

Expand Down Expand Up @@ -146,11 +145,11 @@ int main()
}

/* output resulting metrics */
(void) fprintf(stdout, "name,size,insert,lookup,delete\n");
(void) fprintf(stdout, "name,size,insert,lookup,delete,checksum\n");
for (i = 0; i < vector_size(&metrics); i ++)
{
mp = vector_at(&metrics, i);
(void) fprintf(stdout, "%s,%lu,%f,%f,%f\n", mp->name, mp->size, mp->insert, mp->lookup, mp->delete);
(void) fprintf(stdout, "%s,%lu,%f,%f,%f,%lu\n", mp->name, mp->size, mp->insert, mp->lookup, mp->delete, mp->sum);
}

vector_destruct(&metrics);
Expand Down
10 changes: 0 additions & 10 deletions benchmark/map.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
#ifndef BENCHMARKS_MAP_H_INCLUDED
#define BENCHMARKS_MAP_H_INCLUDED

typedef struct map_metric map_metric;
struct map_metric
{
char *name;
void (*measure)(map_metric *, uint32_t *, size_t);
double insert;
double at;
double erase;
};

uint64_t ntime(void);

#endif /* BENCHMARKS_MAP_H_INCLUDED */
63 changes: 0 additions & 63 deletions benchmark/map_dynamic.c

This file was deleted.

6 changes: 0 additions & 6 deletions benchmark/map_dynamic.h

This file was deleted.

65 changes: 65 additions & 0 deletions benchmark/map_libdynamic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/param.h>
#include <err.h>
#include <time.h>

#include "dynamic.h"
#include "map.h"

typedef struct map_element map_element;
struct map_element
{
uint32_t key;
uint32_t value;
};

static size_t hash(void *e)
{
return *(uint32_t *) e;
}

static int equal(void *e1, void *e2)
{
return *(uint32_t *) e1 == *(uint32_t *) e2;
}

static void set(void *e1, void *e2)
{
*(uint64_t *) e1 = *(uint64_t *) e2;
}

void map_libdynamic(int *keys, int *keys_shuffled, int *values, size_t size, size_t lookups, double *insert, double *lookup, double *erase, uint64_t *sum)
{
map m;
uint64_t t1, t2, s;
size_t i, n;

map_construct(&m, sizeof(map_element), (map_element[]) {{.key = -1}}, set);

t1 = ntime();
for (i = 0; i < size; i ++)
map_insert(&m, (map_element[]) {{.key = keys[i], .value = values[i]}}, hash, equal, set, NULL);
t2 = ntime();
*insert = (double) (t2 - t1) / size;

s = 0;
t1 = ntime();
for (n = lookups; n; n -= i)
for (i = 0; i < MIN(size, n); i ++)
s += ((map_element *) map_at(&m, (map_element[]){{.key = keys_shuffled[i]}}, hash, equal))->value;
t2 = ntime();
*sum = s;
*lookup = (double) (t2 - t1) / lookups;

t1 = ntime();
for (i = 0; i < size; i ++)
map_erase(&m, (map_element[]){{.key = keys[i]}}, hash, equal, set, NULL);
t2 = ntime();
if (map_size(&m))
errx(1, "inconsistency");
*erase = (double) (t2 - t1) / size;

map_destruct(&m, NULL, NULL);
}
6 changes: 6 additions & 0 deletions benchmark/map_libdynamic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef MAP_LIBDYNAMIC_H_INCLUDED
#define MAP_LIBDYNAMIC_H_INCLUDED

void map_libdynamic(int *, int *, int *, size_t, size_t, double *, double *, double *, uint64_t *);

#endif /* MAP_LIBDYNAMIC_H_INCLUDED */
34 changes: 18 additions & 16 deletions benchmark/map_subclass.c → benchmark/map_libdynamic_subclass.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/param.h>
#include <time.h>
#include <err.h>

Expand Down Expand Up @@ -64,35 +64,37 @@ static size_t map_int_pair_size(map_int_pair *m)
return map_size(m);
}

void map_subclass(map_metric *metric, uint32_t *a, size_t n)
void map_libdynamic_subclass(int *keys, int *keys_shuffled, int *values, size_t size, size_t lookups,
double *insert, double *lookup, double *erase, uint64_t *sum)
{
map_int_pair m;
uint64_t t1, t2;
size_t i;
map m;
uint64_t t1, t2, s;
size_t i, n;

map_int_pair_construct(&m);

t1 = ntime();
for (i = 0; i < n; i ++)
map_int_pair_insert(&m, a[i], 1);
for (i = 0; i < size; i ++)
map_int_pair_insert(&m, keys[i], values[i]);
t2 = ntime();
metric->insert = (double) (t2 - t1) / n;
*insert = (double) (t2 - t1) / size;

s = 0;
t1 = ntime();
for (i = 0; i < n; i ++)
if (map_int_pair_at(&m, a[i]) != 1)
errx(1, "inconsistency");
for (n = lookups; n; n -= i)
for (i = 0; i < MIN(size, n); i ++)
s += map_int_pair_at(&m, keys_shuffled[i]);
t2 = ntime();
metric->at = (double) (t2 - t1) / n;
*sum = s;
*lookup = (double) (t2 - t1) / lookups;

t1 = ntime();
for (i = 0; i < n; i ++)
map_int_pair_erase(&m, a[i]);
for (i = 0; i < size; i ++)
map_int_pair_erase(&m, keys[i]);
t2 = ntime();
if (map_int_pair_size(&m))
errx(1, "inconsistency");
metric->erase = (double) (t2 - t1) / n;

*erase = (double) (t2 - t1) / size;

map_int_pair_destruct(&m);
}
6 changes: 6 additions & 0 deletions benchmark/map_libdynamic_subclass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef MAP_LIBDYNAMIC_SUBCLASS_H_INCLUDED
#define MAP_LIBDYNAMIC_SUBCLASS_H_INCLUDED

void map_libdynamic_subclass(int *, int *, int *, size_t, size_t, double *, double *, double *, uint64_t *);

#endif /* MAP_LIBDYNAMIC_SUBCLASS_H_INCLUDED */
6 changes: 0 additions & 6 deletions benchmark/map_subclass.h

This file was deleted.

0 comments on commit 1982366

Please sign in to comment.