diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/csmap_ex2.c | 60 |
1 files changed, 48 insertions, 12 deletions
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 <stc/csmap.h>
#include <stc/crandom.h>
#include <stdio.h>
+#include <time.h>
+#ifdef __cplusplus
+#include <map>
+#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<int, int> 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 |
