summaryrefslogtreecommitdiffhomepage
path: root/benchmarks/csmap_benchmark.cpp
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-02-10 23:17:01 +0100
committerTyge Løvset <[email protected]>2021-02-10 23:17:01 +0100
commitacd1c5560ac49b0812e258d35107603b7c8fe6f7 (patch)
treec20409204389c4c3c538aab24c16f15165210c0a /benchmarks/csmap_benchmark.cpp
parentb9485b4410d2cb3d81766c487dd0edd0e92868d4 (diff)
downloadSTC-modified-acd1c5560ac49b0812e258d35107603b7c8fe6f7.tar.gz
STC-modified-acd1c5560ac49b0812e258d35107603b7c8fe6f7.zip
Benchmark image added.
Diffstat (limited to 'benchmarks/csmap_benchmark.cpp')
-rw-r--r--benchmarks/csmap_benchmark.cpp129
1 files changed, 129 insertions, 0 deletions
diff --git a/benchmarks/csmap_benchmark.cpp b/benchmarks/csmap_benchmark.cpp
new file mode 100644
index 00000000..d0214fd2
--- /dev/null
+++ b/benchmarks/csmap_benchmark.cpp
@@ -0,0 +1,129 @@
+#include <stdio.h>
+#include <time.h>
+#include <stc/crandom.h>
+#include <stc/csmap.h>
+
+#ifdef __cplusplus
+#include <map>
+#endif
+
+enum {INSERT, ERASE, FIND, ITER, DESTRUCT, N_TESTS};
+const char* operations[] = {"insert", "erase", "find", "iter", "destruct"};
+typedef struct { time_t t1, t2; uint64_t sum; float fac; } Range;
+typedef struct { const char* name; Range test[N_TESTS]; } Sample;
+enum {SAMPLES = 2, N = 4000000};
+
+uint64_t seed = 1, mask1 = 0xfffffff;
+
+static float secs(Range s) { return (float)(s.t2 - s.t1) / CLOCKS_PER_SEC; }
+
+using_csmap(x, size_t, size_t);
+
+#ifdef __cplusplus
+Sample test_std_map() {
+ typedef std::map<size_t, size_t> container;
+ Sample s = {"std,map"};
+ {
+ stc64_srandom(seed);
+ s.test[INSERT].t1 = clock();
+ container con;
+ c_forrange (i, N/2) con.emplace(stc64_random() & mask1, i);
+ c_forrange (i, N/2) con.emplace(i, i);
+ s.test[INSERT].t2 = clock();
+ s.test[INSERT].sum = con.size();
+ stc64_srandom(seed);
+ s.test[ERASE].t1 = clock();
+ c_forrange (N) con.erase(stc64_random() & mask1);
+ s.test[ERASE].t2 = clock();
+ s.test[ERASE].sum = con.size();
+ }{
+ container con;
+ stc64_srandom(seed);
+ c_forrange (i, N/2) con.emplace(stc64_random() & mask1, i);
+ c_forrange (i, N/2) con.emplace(i, i);
+ stc64_srandom(seed);
+ s.test[FIND].t1 = clock();
+ size_t sum = 0;
+ container::iterator it;
+ c_forrange (N) if ((it = con.find(stc64_random() & mask1)) != con.end()) sum += it->second;
+ s.test[FIND].t2 = clock();
+ s.test[FIND].sum = sum;
+ s.test[ITER].t1 = clock();
+ sum = 0;
+ for (auto i: con) sum += i.second;
+ s.test[ITER].t2 = clock();
+ s.test[ITER].sum = sum;
+ s.test[DESTRUCT].t1 = clock();
+ }
+ s.test[DESTRUCT].t2 = clock();
+ s.test[DESTRUCT].sum = 0;
+ return s;
+}
+#else
+Sample test_std_map() { Sample s = {"std-map"}; return s;}
+#endif
+
+
+
+Sample test_stc_map() {
+ typedef csmap_x container;
+ Sample s = {"STC,map"};
+ {
+ stc64_srandom(seed);
+ s.test[INSERT].t1 = clock();
+ container con = csmap_x_init();
+ c_forrange (i, N/2) csmap_x_emplace(&con, stc64_random() & mask1, i);
+ c_forrange (i, N/2) csmap_x_emplace(&con, i, i);
+ s.test[INSERT].t2 = clock();
+ s.test[INSERT].sum = csmap_x_size(con);
+ stc64_srandom(seed);
+ s.test[ERASE].t1 = clock();
+ c_forrange (N) csmap_x_erase(&con, stc64_random() & mask1);
+ s.test[ERASE].t2 = clock();
+ s.test[ERASE].sum = csmap_x_size(con);
+ csmap_x_del(&con);
+ }{
+ container con = csmap_x_init();
+ stc64_srandom(seed);
+ c_forrange (i, N/2) csmap_x_emplace(&con, stc64_random() & mask1, i);
+ c_forrange (i, N/2) csmap_x_emplace(&con, i, i);
+ stc64_srandom(seed);
+ s.test[FIND].t1 = clock();
+ size_t sum = 0;
+ csmap_x_iter_t it;
+ c_forrange (N) if ((it = csmap_x_find(&con, stc64_random() & mask1)).ref) sum += it.ref->second;
+ s.test[FIND].t2 = clock();
+ s.test[FIND].sum = sum;
+ s.test[ITER].t1 = clock();
+ sum = 0;
+ c_foreach (i, csmap_x, con) sum += i.ref->second;
+ s.test[ITER].t2 = clock();
+ s.test[ITER].sum = sum;
+ s.test[DESTRUCT].t1 = clock();
+ csmap_x_del(&con);
+ }
+ s.test[DESTRUCT].t2 = clock();
+ s.test[DESTRUCT].sum = 0;
+ return s;
+}
+
+int main(int argc, char* argv[])
+{
+ Sample std_s[SAMPLES + 1], stc_s[SAMPLES + 1];
+ c_forrange (i, int, SAMPLES) {
+ std_s[i] = test_std_map();
+ stc_s[i] = test_stc_map();
+ if (i > 0) c_forrange (j, int, N_TESTS) {
+ if (secs(std_s[i].test[j]) < secs(std_s[0].test[j])) std_s[0].test[j] = std_s[i].test[j];
+ if (secs(stc_s[i].test[j]) < secs(stc_s[0].test[j])) stc_s[0].test[j] = stc_s[i].test[j];
+ if (stc_s[i].test[j].sum != stc_s[0].test[j].sum) printf("Error in sum: test %d, sample %d\n", i, j);
+ }
+ }
+ float std_sum = 0, stc_sum = 0;
+ c_forrange (j, N_TESTS) { std_sum += secs(std_s[0].test[j]); stc_sum += secs(stc_s[0].test[j]); }
+ if (argv[1][0] == '1') printf("compiler,library,container,count,operation,time,ratio\n");
+ c_forrange (j, N_TESTS) printf("%s,%s,%d,%s,%.3f,%.3f\n", argv[2], std_s[0].name, N, operations[j], secs(std_s[0].test[j]), 1.0f);
+ printf("%s,%s,%d,%s,%.3f,%.3f\n", argv[2], std_s[0].name, N, "total", std_sum, 1.0f);
+ c_forrange (j, N_TESTS) printf("%s,%s,%d,%s,%.3f,%.3f\n", argv[2], stc_s[0].name, N, operations[j], secs(stc_s[0].test[j]), secs(std_s[0].test[j]) ? secs(stc_s[0].test[j])/secs(std_s[0].test[j]) : 1.0f);
+ printf("%s,%s,%d,%s,%.3f,%.3f\n", argv[2], stc_s[0].name, N, "total", stc_sum, stc_sum/std_sum);
+}