From 831a951ab8efc969e843e71fb0ca73650c3bba4e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 2 Feb 2021 21:37:11 +0100 Subject: Improved csmap_ex2, and fixed README.md nagging bug. --- examples/csmap_ex2.c | 60 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/csmap_ex2.c b/examples/csmap_ex2.c index 3c2d6cc7..ebb0f6cf 100644 --- a/examples/csmap_ex2.c +++ b/examples/csmap_ex2.c @@ -1,21 +1,57 @@ #include #include #include +#include +#ifdef __cplusplus +#include +#endif using_csmap(i, int, int); - +enum {N=10000000}; int main() { - csmap_i map = csmap_i_init(); - stc64_srandom(1); - c_forrange (i, 1000000) csmap_i_emplace(&map, stc64_random() & 0xffffff, i); - - size_t n = 0, sum = 0; - c_foreach (i, csmap_i, map) { - sum += i.ref->first; - if (n++ < 20) printf("%d: %d\n", i.ref->first, i.ref->second); + clock_t t1, t2, t3, t4, t5; + { + t1 = clock(); + csmap_i map = csmap_i_init(); + stc64_srandom(1); + c_forrange (i, N) + csmap_i_emplace(&map, stc64_random() & 0xffffff, i); + c_forrange (i, N/2) + csmap_i_erase(&map, stc64_random() & 0xffffff); + t2 = clock(); + size_t n = 0, sum = 0; + c_foreach (i, csmap_i, map) + sum += i.ref->first; + t3 = clock(); + c_foreach (i, csmap_i, map) + if (n++ < 20) printf("%d: %d\n", i.ref->first, i.ref->second); else break; + printf("size %zu: %zu\n", csmap_i_size(map), sum); + t4 = clock(); + csmap_i_del(&map); + } + t5 = clock(); + printf("emplace: %g sec, sum: %g sec, destruct: %g sec\n", (float)(t2-t1)/CLOCKS_PER_SEC, (float)(t3-t2)/CLOCKS_PER_SEC, (float)(t5-t4)/CLOCKS_PER_SEC); +#ifdef __cplusplus + { + t1 = clock(); + std::map map; + stc64_srandom(1); + c_forrange (i, N) + map.emplace(stc64_random() & 0xffffff, i); + c_forrange (i, N/2) + map.erase(stc64_random() & 0xffffff); + t2 = clock(); + size_t n = 0, sum = 0; + for (auto i: map) + sum += i.first; + t3 = clock(); + for (auto i: map) + if (n++ < 20) printf("%d: %d\n", i.first, i.second); else break; + printf("size %zu: %zu\n", map.size(), sum); + t4 = clock(); } - printf("size %zu: %zu\n", csmap_i_size(map), sum); - csmap_i_del(&map); - puts("done"); + t5 = clock(); + printf("emplace %g sec, sum: %g sec, destruct: %g sec\n", (float)(t2-t1)/CLOCKS_PER_SEC, (float)(t3-t2)/CLOCKS_PER_SEC, (float)(t5-t4)/CLOCKS_PER_SEC); +#endif } \ No newline at end of file -- cgit v1.2.3