| 10 | #include "tinyformat.h" |
| 11 | |
| 12 | void speedTest(const std::string& which) |
| 13 | { |
| 14 | // Following is required so that we're not limited by per-character |
| 15 | // buffering. |
| 16 | std::ios_base::sync_with_stdio(false); |
| 17 | const long maxIter = 2000000L; |
| 18 | if(which == "printf") |
| 19 | { |
| 20 | // libc version |
| 21 | for(long i = 0; i < maxIter; ++i) |
| 22 | printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n", |
| 23 | 1.234, 42, 3.13, "str", (void*)1000, (int)'X'); |
| 24 | } |
| 25 | else if(which == "iostreams") |
| 26 | { |
| 27 | // Std iostreams version. What a mess!! |
| 28 | for(long i = 0; i < maxIter; ++i) |
| 29 | std::cout |
| 30 | << std::setprecision(10) << std::fixed << 1.234 |
| 31 | << std::resetiosflags(std::ios::floatfield) << ":" |
| 32 | << std::setw(4) << std::setfill('0') << 42 << std::setfill(' ') << ":" |
| 33 | << std::setiosflags(std::ios::showpos) << 3.13 << std::resetiosflags(std::ios::showpos) << ":" |
| 34 | << "str" << ":" |
| 35 | << (void*)1000 << ":" |
| 36 | << 'X' << ":%\n"; |
| 37 | } |
| 38 | else if(which == "tinyformat") |
| 39 | { |
| 40 | // tinyformat version. |
| 41 | for(long i = 0; i < maxIter; ++i) |
| 42 | tfm::printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n", |
| 43 | 1.234, 42, 3.13, "str", (void*)1000, (int)'X'); |
| 44 | } |
| 45 | else if(which == "boost") |
| 46 | { |
| 47 | // boost::format version |
| 48 | for(long i = 0; i < maxIter; ++i) |
| 49 | std::cout << boost::format("%0.10f:%04d:%+g:%s:%p:%c:%%\n") |
| 50 | % 1.234 % 42 % 3.13 % "str" % (void*)1000 % (int)'X'; |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | assert(0 && "speed test for which version?"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | int main(int argc, char* argv[]) |