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. --- README.md | 10 ++++----- examples/csmap_ex2.c | 60 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f63f5f4e..027df4ae 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Others: - [***coption*** - Implements ***coption_get()***, similar to posix **getopt_long()**](docs/coption_api.md) - [***crandom*** - A novel, extremely fast *PRNG* named **stc64**](docs/crandom_api.md) -The usage of the containers is similar to the c++ standard containers in STL, so it should be easy if you are familiar with them. +The usage of the containers is similar to the c++ standard containers in STL, so it should be easy if you are familiar with them. All containers are generic/templated, except for **cstr** and **cbits**. No casting is used, so containers are typesafe like templates in c++. A basic usage example: ```c @@ -43,10 +43,10 @@ int main(void) { cvec_i_push_back(&vec, 10); cvec_i_push_back(&vec, 20); cvec_i_push_back(&vec, 30); - + c_foreach (i, cvec_i, vec) printf(" %d", *i.ref); - + cvec_i_del(&vec); } ``` @@ -73,11 +73,11 @@ using_csmap(i, int, int); // sorted map int main(void) { // define and initialize c_init (cset_i, set, {10, 20, 30}); - c_init (cvec_p, vec, {{10, 1}, {20, 2}, {30, 3}}); + c_init (cvec_p, vec, { {10, 1}, {20, 2}, {30, 3} }); c_init (cdeq_i, deq, {10, 20, 30}); c_init (clist_i, lst, {10, 20, 30}); c_init (cqueue_i, que, {10, 20, 30}); - c_init (csmap_i, map, {{20, 2}, {10, 1}, {30, 3}}); + c_init (csmap_i, map, { {20, 2}, {10, 1}, {30, 3} }); // add one more element cset_i_insert(&set, 40); 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