#include "pch.h" #define NOMINMAX #include #include #include #include #include #include #include #include #include "Windows.h" #pragma comment(lib, "Winmm.lib") using namespace std; #define STRING //#define STRING_C // STRING_C not yet implemented... I hate char*... #ifdef STRING #ifdef STRING_C const int IMAX = numeric_limits::max() / 200; // #else const int IMAX = numeric_limits::max() / 200; // 1000 is about the same time as 200 for C#... #endif #else const int IMAX = numeric_limits::max() / 200; // 100 is the limit on my machine (Core2Duo, 2GB ram), 200 in C# #endif int main() { #ifdef STRING #ifdef STRING_C cout << "C++ Working with " << IMAX << " string values, C-style.\n" << endl; #else cout << "C++ Working with " << IMAX << " string values, C++-style.\n" << endl; #endif #else cout << "C++ Working with " << IMAX << " double values.\n" << endl; #endif timeBeginPeriod(1); double value; unsigned long resultTime; unsigned long startTime; ostringstream strstream; #ifdef STRING string tvalue; #else double tvalue; #endif #pragma region vector benchmarks { #ifdef STRING vector list; #else vector list; #endif cout << "vector growing at the end" << endl; startTime = timeGetTime(); for (int i = IMAX; i >= 0; i--) { value = i % 100 * i / 100; #ifdef STRING #ifdef STRING_C #else strstream << value; list.push_back(strstream.str()); strstream.str(""); #endif #else list.push_back(value); #endif } resultTime = timeGetTime() - startTime; cout << " inserts:\t " << resultTime << " ms" << endl; startTime = timeGetTime(); for (int i = 0; i < IMAX; i++) { tvalue = list[i]; } resultTime = timeGetTime() - startTime; cout << " reads:\t\t " << resultTime << " ms" << endl; } #pragma endregion cout << endl; #pragma region Hashtable benchmarks { //Dictionary dict = new Dictionary(); #ifdef STRING map dict; #else map dict; #endif cout << "map" << endl; startTime = timeGetTime(); for (int i = IMAX; i >= 0; i--) { value = i % 100 * i / 100; #ifdef STRING strstream << value; dict[i] = strstream.str(); strstream.str(""); #else dict[i] = value; #endif } resultTime = timeGetTime() - startTime; cout << " inserts:\t " << resultTime << " ms" << endl; startTime = timeGetTime(); for (int i = 0; i < IMAX; i++) { tvalue = dict[i]; } resultTime = timeGetTime() - startTime; cout << " reads:\t\t " << resultTime << " ms" << endl; } #pragma endregion cout << endl; #pragma region list benchmarks { #ifdef STRING list list; #else list list; #endif cout << "list" << endl; startTime = timeGetTime(); for (int i = IMAX; i >= 0; i--) { value = i % 100 * i / 100; #ifdef STRING strstream << value; list.push_front(strstream.str()); strstream.str(""); #else list.push_front(value); #endif } resultTime = timeGetTime() - startTime; cout << " inserts:\t " << resultTime << " ms" << endl; startTime = timeGetTime(); #ifdef STRING std::list::iterator it #else std::list::iterator it #endif = list.begin(); while(it != list.end()) { tvalue = *it; it++; //tvalue = enumerator.Current; } resultTime = timeGetTime() - startTime; cout << " reads:\t\t " << resultTime << " ms (sequence)" << endl; } #pragma endregion cout << endl; #pragma region vector benchmarks { #ifdef STRING vector list; #else vector list; #endif cout << "vector inserting at beginning (press ctrl+c to cancel - this takes ages)" << endl; startTime = timeGetTime(); for (int i = IMAX; i >= 0; i--) { value = i % 100 * i / 100; //list.Insert(0, value.ToString()); #ifdef STRING strstream << value; list.insert(list.begin(), strstream.str()); strstream.str(""); #else list.insert(list.begin(), value); #endif //if (i % 10000 == 0) // cout << ((1 - ((double)i / (double)IMAX)) * 100) << "\r" << flush; } resultTime = timeGetTime() - startTime; cout << " inserts:\t " << resultTime << " ms" << endl; startTime = timeGetTime(); for (int i = 0; i < IMAX; i++) { tvalue = list[i]; } resultTime = timeGetTime() - startTime; cout << " reads:\t\t " << resultTime << " ms" << endl; } #pragma endregion timeEndPeriod(1); return 0; }