From ba19a53823973e9d5abec8d0aaf4ced89ec75425 Mon Sep 17 00:00:00 2001 From: cxl Date: Sat, 14 Jan 2012 17:18:07 +0000 Subject: [PATCH] .developing svo_value (benchmark) git-svn-id: svn://ultimatepp.org/upp/trunk@4420 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- benchmarks/ValueBench/ValueBench.cpp | 187 ++++----- benchmarks/idmapBench/main.cpp | 554 ++++++++++++++------------- 2 files changed, 375 insertions(+), 366 deletions(-) diff --git a/benchmarks/ValueBench/ValueBench.cpp b/benchmarks/ValueBench/ValueBench.cpp index c524db1d6..b97c8e1c7 100644 --- a/benchmarks/ValueBench/ValueBench.cpp +++ b/benchmarks/ValueBench/ValueBench.cpp @@ -1,91 +1,96 @@ -#include - -using namespace Upp; - -#ifdef _DEBUG -#define N 1 -#else -#define N 100000 -#endif - -Value h; - -CONSOLE_APP_MAIN -{ - StdLogSetup(LOG_COUT|LOG_FILE); - Vector x; - Vector xs; - for(int i = 0; i < 100; i++) { - x.Add(Random(1000)); - xs.Add(AsString(1000)); - } - Value xxx = Null; - h = xxx; - RTIMING("Total"); - int sum; - for(int i = 0; i < N; i++) { - ValueArray va; - { - va.Clear(); - RTIMING("Add int"); - for(int i = 0; i < 100; i++) - va.Add(x[i]); - } - { - RTIMING("Read int"); - for(int i = 0; i < va.GetCount(); i++) - sum += (int)va[i]; - } - { - RTIMING("Read int with To"); - for(int i = 0; i < va.GetCount(); i++) - sum += va[i].To(); - } - { - va.Clear(); - RTIMING("Add String"); - for(int i = 0; i < 100; i++) - va.Add(xs[i]); - } - { - RTIMING("Read String"); - for(int i = 0; i < va.GetCount(); i++) - sum += String(va[i]).GetLength(); - } - { - RTIMING("Read String with To"); - for(int i = 0; i < va.GetCount(); i++) - sum += va[i].To().GetLength(); - } - Vector vs; - { - vs.Clear(); - RTIMING("Add Vector"); - for(int i = 0; i < 100; i++) - vs.Add(xs[i]); - } - { - RTIMING("Read Vector"); - for(int i = 0; i < vs.GetCount(); i++) - sum += vs[i].GetLength(); - } - Vector vv; - { - vv.Clear(); - RTIMING("Add Vector String"); - for(int i = 0; i < 100; i++) - vv.Add(xs[i]); - } - { - RTIMING("Read Vector String"); - for(int i = 0; i < vv.GetCount(); i++) - sum += String(vv[i]).GetLength(); - } - { - RTIMING("Read Vector String with To"); - for(int i = 0; i < vv.GetCount(); i++) - sum += vv[i].To().GetLength(); - } - } - Cout() << sum << "\n"; -} +#include + +using namespace Upp; + +#ifdef _DEBUG +#define N 1 +#else +#define N 300000 +#endif + +Value h; + +CONSOLE_APP_MAIN +{ + StdLogSetup(LOG_COUT|LOG_FILE); + Vector x; + Vector xs; + for(int i = 0; i < 100; i++) { + x.Add(Random(1000)); + xs.Add(AsString(1000)); + } + Value xxx = Null; + h = xxx; + RTIMING("Total"); + int sum; + for(int i = 0; i < N; i++) { + ValueArray va; + { + va.Clear(); + RTIMING("Add int"); + for(int i = 0; i < 100; i++) + va.Add(x[i]); + } + { + RTIMING("Read int"); + for(int i = 0; i < va.GetCount(); i++) + sum += (int)va[i]; + } + { + RTIMING("Read int with To"); + for(int i = 0; i < va.GetCount(); i++) + sum += va[i].To(); + } + { + va.Clear(); + RTIMING("Add String"); + for(int i = 0; i < 100; i++) + va.Add(xs[i]); + } + { + RTIMING("Read String"); + for(int i = 0; i < va.GetCount(); i++) + sum += String(va[i]).GetLength(); + } + { + RTIMING("Read String with To"); + for(int i = 0; i < va.GetCount(); i++) + sum += va[i].To().GetLength(); + } + { + RTIMING("Read String with Get"); + for(int i = 0; i < va.GetCount(); i++) + sum += va[i].Get().GetLength(); + } + Vector vs; + { + vs.Clear(); + RTIMING("Add Vector"); + for(int i = 0; i < 100; i++) + vs.Add(xs[i]); + } + { + RTIMING("Read Vector"); + for(int i = 0; i < vs.GetCount(); i++) + sum += vs[i].GetLength(); + } + Vector vv; + { + vv.Clear(); + RTIMING("Add Vector String"); + for(int i = 0; i < 100; i++) + vv.Add(xs[i]); + } + { + RTIMING("Read Vector String"); + for(int i = 0; i < vv.GetCount(); i++) + sum += String(vv[i]).GetLength(); + } + { + RTIMING("Read Vector String with To"); + for(int i = 0; i < vv.GetCount(); i++) + sum += vv[i].To().GetLength(); + } + } + Cout() << sum << "\n"; +} diff --git a/benchmarks/idmapBench/main.cpp b/benchmarks/idmapBench/main.cpp index 38a76d6b3..f08db8a67 100644 --- a/benchmarks/idmapBench/main.cpp +++ b/benchmarks/idmapBench/main.cpp @@ -1,275 +1,279 @@ -#define NDEBUG -#define _SECURE_SCL 0 - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define TEST_HASHMAP - -#ifdef TEST_HASHMAP - -#ifdef COMPILER_GCC -#include -#else -#include -#endif - -#endif - -using namespace std; -using namespace Upp; - -#ifndef _DEBUG -#define NO_OUTPUT // for benchmark purposes, output is omitted -#endif - -void BenchNTL(const char *file) { - FileIn in(file); - if (!in) { - std::cout << "Cannot open input file.\n"; - return; - } - - VectorMap > map; - int line = 1; - - for(;;) { - int c = in.Get(); - if(c < 0) break; - if(IsAlpha(c) || c == '_') { - String id; - id.Cat(c); - c = in.Get(); - while(c >= 0 && (IsAlNum(c) || c == '_')) { - id.Cat(c); - c = in.Get(); - } - map.GetAdd(id).Add(line); - } - else - if(IsDigit(c)) - do c = in.Get(); - while(c >= 0 && (IsAlNum(c) || c == '.')); - if(c == '\n') - ++line; - } - - Vector order = GetSortOrder(map.GetKeys()); -#ifndef NO_OUTPUT - for(int i = 0; i < order.GetCount(); i++) { - std::cout << ~map.GetKey(order[i]) << ": "; - const Vector& l = map[order[i]]; - for(int i = 0; i < l.GetCount(); i++) { - if(i) std::cout << ", "; - std::cout << l[i]; - } - std::cout << '\n'; - } -#endif -} - -bool SimpleValueOrder(const Value& a, const Value& b) -{ - return AsString(a) < AsString(b); -} - -void BenchValueMap(const char *file) { - FileIn in(file); - if (!in) { - std::cout << "Cannot open input file.\n"; - return; - } - - ValueMap map; - int line = 1; - - for(;;) { - int c = in.Get(); - if(c < 0) break; - if(IsAlpha(c) || c == '_') { - String id; - id.Cat(c); - c = in.Get(); - while(c >= 0 && (IsAlNum(c) || c == '_')) { - id.Cat(c); - c = in.Get(); - } - ValueArray va = map.GetAndClear(id); - va.Add(line); - map.Set(id, va); - } - else - if(IsDigit(c)) - do c = in.Get(); - while(c >= 0 && (IsAlNum(c) || c == '.')); - if(c == '\n') - ++line; - } - - Vector order = GetSortOrder(map.GetKeys(), SimpleValueOrder); -#ifndef NO_OUTPUT - for(int i = 0; i < order.GetCount(); i++) { - std::cout << ~(String)(map.GetKeys()[order[i]]) << ": "; - ValueArray l = map.GetValues()[order[i]]; - for(int i = 0; i < l.GetCount(); i++) { - if(i) std::cout << ", "; - std::cout << (int)l[i]; - } - std::cout << '\n'; - } -#endif -} - -template -void BenchSTL(Container& imap, const char *file) { - std::ifstream in(file); - if (!in) { - std::cout << "Cannot open input file.\n"; - return; - } - - int line = 1; - - for(;;) { - int c = in.get(); - if(c == EOF) break; - if(isalpha(c) || c == '_') { - string id; - id += c; - c = in.get(); - while(c != EOF && (isalnum(c) || c == '_')) { - id += c; - c = in.get(); - } - imap[id].push_back(line); - } - else - if(isdigit(c)) - do c = in.get(); - while(c != EOF && (isalnum(c) || c == '.')); - if(c == '\n') - ++line; - } -} - -void BenchMap(const char *file) -{ - map< string, vector > imap; - BenchSTL(imap, file); -#ifndef NO_OUTPUT - map< std::string, vector >::const_iterator e = imap.end(); - for(map< std::string, vector >::const_iterator i = imap.begin(); i != e; i++) { - std::cout << i->first << ": "; - vector::const_iterator e = i->second.end(); - vector::const_iterator b = i->second.begin(); - for(vector::const_iterator j = b; j != e; j++) { - if(j != b) std::cout << ", "; - std::cout << *j; - } - std::cout << '\n'; - } -#endif -} - -#ifdef TEST_HASHMAP - -#ifdef COMPILER_GCC -typedef std::tr1::unordered_map< string, vector > HashMap; -#else -typedef stdext::hash_map< string, vector > HashMap; -#endif - -inline bool h_less(const HashMap::value_type *a, const HashMap::value_type *b) -{ - return a->first < b->first; -} - -void BenchHashMap(const char *file) -{ - HashMap imap; - BenchSTL(imap, file); - vector< const HashMap::value_type * > order; - for(HashMap::const_iterator i = imap.begin(); i != imap.end(); i++) - order.push_back(&*i); - sort(order.begin(), order.end(), h_less); - -#ifndef NO_OUTPUT - vector< const HashMap::value_type * >::const_iterator e = order.end(); - for(vector< const HashMap::value_type * >::const_iterator i = order.begin(); i != e; i++) { - std::cout << (*i)->first << ": "; - vector::const_iterator e = (*i)->second.end(); - vector::const_iterator b = (*i)->second.begin(); - for(vector::const_iterator j = b; j != e; j++) { - if(j != b) std::cout << ", "; - std::cout << *j; - } - std::cout << '\n'; - } -#endif -} - -#endif - -#ifdef _DEBUG -#define N 1 -#else -#define N 10000 -#endif - -CONSOLE_APP_MAIN -{ - String fn; - int argc = CommandLine().GetCount(); - const Vector& argv = CommandLine(); - if(argc < 1) - fn = GetDataFile("main.cpp"); - else - fn = argv[0]; - - BenchValueMap(fn); - cout << "===========================\n"; - BenchNTL(fn); // first run to cache the file - -#ifdef TEST_HASHMAP - { - BenchHashMap(fn); - TimeStop tm; - for(int n = 0; n < N; n++) - BenchHashMap(fn); - cout << "STL hash_map time: " << tm.Elapsed() << " ms \n"; - } -#endif - - { - BenchMap(fn); - TimeStop tm; - for(int n = 0; n < N; n++) - BenchMap(fn); - cout << "STL map time: " << tm.Elapsed() << " ms \n"; - } - - { - BenchNTL(fn); - TimeStop tm; - for(int n = 0; n < N; n++) - BenchNTL(fn); - cout << "NTL time: " << tm.Elapsed() << " ms\n"; - } - - { - BenchValueMap(fn); - TimeStop tm; - for(int n = 0; n < N; n++) - BenchValueMap(fn); - cout << "ValueMap time: " << tm.Elapsed() << " ms\n"; - } -} +#define NDEBUG +#define _SECURE_SCL 0 + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TEST_HASHMAP + +#ifdef TEST_HASHMAP + +#ifdef COMPILER_GCC +#include +#else +#include +#endif + +#endif + +using namespace std; +using namespace Upp; + +#ifndef _DEBUG +#define NO_OUTPUT // for benchmark purposes, output is omitted +#endif + +void BenchNTL(const char *file) { + FileIn in(file); + if (!in) { + std::cout << "Cannot open input file.\n"; + return; + } + + VectorMap > map; + int line = 1; + + for(;;) { + int c = in.Get(); + if(c < 0) break; + if(IsAlpha(c) || c == '_') { + String id; + id.Cat(c); + c = in.Get(); + while(c >= 0 && (IsAlNum(c) || c == '_')) { + id.Cat(c); + c = in.Get(); + } + map.GetAdd(id).Add(line); + } + else + if(IsDigit(c)) + do c = in.Get(); + while(c >= 0 && (IsAlNum(c) || c == '.')); + if(c == '\n') + ++line; + } + + Vector order = GetSortOrder(map.GetKeys()); +#ifndef NO_OUTPUT + for(int i = 0; i < order.GetCount(); i++) { + std::cout << ~map.GetKey(order[i]) << ": "; + const Vector& l = map[order[i]]; + for(int i = 0; i < l.GetCount(); i++) { + if(i) std::cout << ", "; + std::cout << l[i]; + } + std::cout << '\n'; + } +#endif +} + +bool SimpleValueOrder(const Value& a, const Value& b) +{ + return AsString(a) < AsString(b); +} + +void BenchValueMap(const char *file) { + FileIn in(file); + if (!in) { + std::cout << "Cannot open input file.\n"; + return; + } + + ValueMap map; + int line = 1; + + for(;;) { + int c = in.Get(); + if(c < 0) break; + if(IsAlpha(c) || c == '_') { + String id; + id.Cat(c); + c = in.Get(); + while(c >= 0 && (IsAlNum(c) || c == '_')) { + id.Cat(c); + c = in.Get(); + } +#ifdef SVO_VALUE + ValueArray va = map.GetAndClear(id); +#else + ValueArray va = map[id]; +#endif + va.Add(line); + map.Set(id, va); + } + else + if(IsDigit(c)) + do c = in.Get(); + while(c >= 0 && (IsAlNum(c) || c == '.')); + if(c == '\n') + ++line; + } + + Vector order = GetSortOrder(map.GetKeys(), SimpleValueOrder); +#ifndef NO_OUTPUT + for(int i = 0; i < order.GetCount(); i++) { + std::cout << ~(String)(map.GetKeys()[order[i]]) << ": "; + ValueArray l = map.GetValues()[order[i]]; + for(int i = 0; i < l.GetCount(); i++) { + if(i) std::cout << ", "; + std::cout << (int)l[i]; + } + std::cout << '\n'; + } +#endif +} + +template +void BenchSTL(Container& imap, const char *file) { + std::ifstream in(file); + if (!in) { + std::cout << "Cannot open input file.\n"; + return; + } + + int line = 1; + + for(;;) { + int c = in.get(); + if(c == EOF) break; + if(isalpha(c) || c == '_') { + string id; + id += c; + c = in.get(); + while(c != EOF && (isalnum(c) || c == '_')) { + id += c; + c = in.get(); + } + imap[id].push_back(line); + } + else + if(isdigit(c)) + do c = in.get(); + while(c != EOF && (isalnum(c) || c == '.')); + if(c == '\n') + ++line; + } +} + +void BenchMap(const char *file) +{ + map< string, vector > imap; + BenchSTL(imap, file); +#ifndef NO_OUTPUT + map< std::string, vector >::const_iterator e = imap.end(); + for(map< std::string, vector >::const_iterator i = imap.begin(); i != e; i++) { + std::cout << i->first << ": "; + vector::const_iterator e = i->second.end(); + vector::const_iterator b = i->second.begin(); + for(vector::const_iterator j = b; j != e; j++) { + if(j != b) std::cout << ", "; + std::cout << *j; + } + std::cout << '\n'; + } +#endif +} + +#ifdef TEST_HASHMAP + +#ifdef COMPILER_GCC +typedef std::tr1::unordered_map< string, vector > HashMap; +#else +typedef stdext::hash_map< string, vector > HashMap; +#endif + +inline bool h_less(const HashMap::value_type *a, const HashMap::value_type *b) +{ + return a->first < b->first; +} + +void BenchHashMap(const char *file) +{ + HashMap imap; + BenchSTL(imap, file); + vector< const HashMap::value_type * > order; + for(HashMap::const_iterator i = imap.begin(); i != imap.end(); i++) + order.push_back(&*i); + sort(order.begin(), order.end(), h_less); + +#ifndef NO_OUTPUT + vector< const HashMap::value_type * >::const_iterator e = order.end(); + for(vector< const HashMap::value_type * >::const_iterator i = order.begin(); i != e; i++) { + std::cout << (*i)->first << ": "; + vector::const_iterator e = (*i)->second.end(); + vector::const_iterator b = (*i)->second.begin(); + for(vector::const_iterator j = b; j != e; j++) { + if(j != b) std::cout << ", "; + std::cout << *j; + } + std::cout << '\n'; + } +#endif +} + +#endif + +#ifdef _DEBUG +#define N 1 +#else +#define N 10000 +#endif + +CONSOLE_APP_MAIN +{ + String fn; + int argc = CommandLine().GetCount(); + const Vector& argv = CommandLine(); + if(argc < 1) + fn = GetDataFile("main.cpp"); + else + fn = argv[0]; + + BenchValueMap(fn); + cout << "===========================\n"; + BenchNTL(fn); // first run to cache the file + +#ifdef TEST_HASHMAP + { + BenchHashMap(fn); + TimeStop tm; + for(int n = 0; n < N; n++) + BenchHashMap(fn); + cout << "STL hash_map time: " << tm.Elapsed() << " ms \n"; + } +#endif + + { + BenchMap(fn); + TimeStop tm; + for(int n = 0; n < N; n++) + BenchMap(fn); + cout << "STL map time: " << tm.Elapsed() << " ms \n"; + } + + { + BenchNTL(fn); + TimeStop tm; + for(int n = 0; n < N; n++) + BenchNTL(fn); + cout << "NTL time: " << tm.Elapsed() << " ms\n"; + } + + { + BenchValueMap(fn); + TimeStop tm; + for(int n = 0; n < N; n++) + BenchValueMap(fn); + cout << "ValueMap time: " << tm.Elapsed() << " ms\n"; + } +}