From 9f14a7d7cd2c8eb29af3e6cd4b6e06b71ca8c28a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 5 Aug 2007 06:10:36 +0000 Subject: Added benchmark proving Raul::Table is slower than std::map and thus a useless piece of crap. Awesome. git-svn-id: http://svn.drobilla.net/lad/raul@678 a436a847-0d15-0410-975c-d299462d15a1 --- tests/Makefile.am | 2 +- tests/table_test.cpp | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index f18bcc9..0829d81 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,6 @@ if BUILD_TESTS -AM_CXXFLAGS = -I.. -lpthread @REDLAND_CFLAGS@ @GLIBMM_CFLAGS@ -DTABLE_SORT_DEBUG +AM_CXXFLAGS = -I.. -lpthread @REDLAND_CFLAGS@ @GLIBMM_CFLAGS@# -DTABLE_SORT_DEBUG ALL_LIBS = @REDLAND_LIBS@ @GLIBMM_LIBS@ ../src/libraul.la noinst_PROGRAMS = \ diff --git a/tests/table_test.cpp b/tests/table_test.cpp index 19fd576..3c1462d 100644 --- a/tests/table_test.cpp +++ b/tests/table_test.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include #include @@ -17,10 +19,17 @@ bool range_comparator(const int& a, const int& b) return ret; } +void benchmark(size_t n); int -main() +main(int argc, char** argv) { + if (argc == 3 && !strcmp(argv[1], "-b")) { + benchmark(atoi(argv[2])); + return 0; + } + + cout << "run with -b num_elems to benchmark" << endl; srand(time(NULL)); range_end_val = rand()%10; @@ -193,3 +202,110 @@ main() return 0; } + +void +benchmark(size_t n) +{ + cout << "Benchmarking with n = " << n << endl; + + int useless_accumulator = 0; + + srand(time(NULL)); + + vector values(n); + for (size_t i=0; i < n; ++i) + values.push_back(rand() % n*100); + + timeval t1; + t1.tv_sec=0; + t1.tv_usec=0; + + timeval t2; + t2.tv_sec=0; + t2.tv_usec=0; + + + /** std::map **/ + + std::map m; + + gettimeofday(&t1, NULL); + + for (size_t i=0; i < n; ++i) + m.insert(make_pair(values[i], values[i])); + + gettimeofday(&t2, NULL); + + float delta_t = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) * 0.000001f; + + cout << "std::map time to insert " << n << " values: \t" << delta_t << endl; + + gettimeofday(&t1, NULL); + + for (size_t i=0; i < n; ++i) + useless_accumulator += m.find(values[i])->second; + + gettimeofday(&t2, NULL); + + delta_t = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) * 0.000001f; + + cout << "std::map time to lookup " << n << " values: \t" << delta_t << endl; + + + /** sorted std::vector **/ + + /*std::vector v; + + gettimeofday(&t1, NULL); + + for (size_t i=0; i < n; ++i) + v.push_back(values[i]); + + sort(v.begin(), v.end()); + + gettimeofday(&t2, NULL); + + delta_t = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) * 0.000001f; + + cout << "std::vector (sorted) time to insert " << n << " values: \t" << delta_t << endl; + + gettimeofday(&t1, NULL); + + for (size_t i=0; i < n; ++i) + useless_accumulator += *lower_bound(v.begin(), v.end(), values[i]); + + gettimeofday(&t2, NULL); + + delta_t = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) * 0.000001f; + + cout << "std::vector (sorted) time to lookup " << n << " values: \t" << delta_t << endl;*/ + + + /** Raul::Table **/ + + Raul::Table t(n); + + gettimeofday(&t1, NULL); + + for (size_t i=0; i < n; ++i) + t.insert(make_pair(values[i], values[i])); + + gettimeofday(&t2, NULL); + + delta_t = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) * 0.000001f; + + cout << "Raul::Table time to insert " << n << " values: " << delta_t << endl; + + gettimeofday(&t1, NULL); + + for (size_t i=0; i < n; ++i) + useless_accumulator += t.find(values[i])->second; + + gettimeofday(&t2, NULL); + + delta_t = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) * 0.000001f; + + cout << "Raul::Table time to lookup " << n << " values: \t" << delta_t << endl; + +} + -- cgit v1.2.1