| 1 | #include "../conversion/stringbuilder.h" |
| 2 | #include "../conversion/stringconversion.h" |
| 3 | #include "../chrono/datetime.h" |
| 4 | |
| 5 | #include <vector> |
| 6 | #include <iostream> |
| 7 | #include <sstream> |
| 8 | |
| 9 | using namespace std; |
| 10 | using namespace ConversionUtilities; |
| 11 | using namespace ChronoUtilities; |
| 12 | |
| 13 | int main() |
| 14 | { |
| 15 | cout << "Benchmarking string builder vs. stringstream" << endl; |
| 16 | |
| 17 | constexpr unsigned int iterations = 500000; |
| 18 | constexpr unsigned int iterations2 = 50; |
| 19 | |
| 20 | vector<string> v1, v2; |
| 21 | v1.reserve(iterations); |
| 22 | v2.reserve(iterations); |
| 23 | |
| 24 | DateTime t1 = DateTime::exactGmtNow(); |
| 25 | for(unsigned int r = 0; r < iterations2; ++r) { |
| 26 | for(unsigned int i = 0; i < iterations; ++i) { |
| 27 | stringstream ss; |
| 28 | v1.emplace_back("left. "s + numberToString(i + 1) + "; right: "s + numberToString(i + 2) + "; top: "s + numberToString(i + 3) + "; bottom: "s + numberToString(i + 4) + ';'); |
| 29 | } |
| 30 | v1.clear(); |
| 31 | } |
| 32 | DateTime t2 = DateTime::exactGmtNow(); |
| 33 | cout << "plus operator: " << (t2 - t1).toString(TimeSpanOutputFormat::Normal, true) << endl; |
| 34 | |
| 35 | t1 = DateTime::exactGmtNow(); |
| 36 | for(unsigned int r = 0; r < iterations2; ++r) { |
| 37 | for(unsigned int i = 0; i < iterations; ++i) { |
| 38 | stringstream ss; |
| 39 | ss << "left: "s << (i + 1) << "; right: "s << (i + 2) << "; top: "s << (i + 3) << "; bottom: "s << (i + 4) << ';'; |
| 40 | v1.emplace_back(ss.str()); |
| 41 | } |
| 42 | v1.clear(); |
| 43 | } |
| 44 | t2 = DateTime::exactGmtNow(); |
| 45 | const TimeSpan diff1 = t2 - t1; |
| 46 | cout << "stringstream: " << diff1.toString(TimeSpanOutputFormat::Normal, true) << endl; |
| 47 | |
| 48 | t1 = DateTime::exactGmtNow(); |
| 49 | for(unsigned int r = 0; r < iterations2; ++r) { |
| 50 | for(unsigned int i = 0; i < iterations; ++i) { |
| 51 | v2.emplace_back("left. "s % (i + 1) % "; right: "s % (i + 2) % "; top: "s % (i + 3) % "; bottom: "s % (i + 4) + ';'); |
| 52 | } |
| 53 | v2.clear(); |
| 54 | } |
| 55 | t2 = DateTime::exactGmtNow(); |
| 56 | const TimeSpan diff2 = t2 - t1; |
| 57 | cout << "string builder: " << diff2.toString(TimeSpanOutputFormat::Normal, true) << endl; |
| 58 | |
| 59 | v1.swap(v2); |
| 60 | |
| 61 | cout << "diff (stringstream minus string builder): " << (diff1 - diff2).toString(TimeSpanOutputFormat::Normal, true) << endl; |
| 62 | cout << "factor (stringstream / string builder): " << (static_cast<double>(diff1.totalTicks()) / diff2.totalTicks()) << endl; |
| 63 | |
| 64 | return 0; |
| 65 | } |