-
Notifications
You must be signed in to change notification settings - Fork 75
/
tinyformat_speed_test.cpp
64 lines (60 loc) · 1.94 KB
/
tinyformat_speed_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#if defined(__linux__) && defined(__clang__)
// Workaround for bug in gcc 4.4 standard library headers when compling with
// clang in C++11 mode.
namespace std { class type_info; }
#endif
#include <boost/format.hpp>
#include <iomanip>
#include <stdio.h>
#include "tinyformat.h"
void speedTest(const std::string& which)
{
// Following is required so that we're not limited by per-character
// buffering.
std::ios_base::sync_with_stdio(false);
const long maxIter = 2000000L;
if(which == "printf")
{
// libc version
for(long i = 0; i < maxIter; ++i)
printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n",
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
}
else if(which == "iostreams")
{
// Std iostreams version. What a mess!!
for(long i = 0; i < maxIter; ++i)
std::cout
<< std::setprecision(10) << std::fixed << 1.234
<< std::resetiosflags(std::ios::floatfield) << ":"
<< std::setw(4) << std::setfill('0') << 42 << std::setfill(' ') << ":"
<< std::setiosflags(std::ios::showpos) << 3.13 << std::resetiosflags(std::ios::showpos) << ":"
<< "str" << ":"
<< (void*)1000 << ":"
<< 'X' << ":%\n";
}
else if(which == "tinyformat")
{
// tinyformat version.
for(long i = 0; i < maxIter; ++i)
tfm::printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n",
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
}
else if(which == "boost")
{
// boost::format version
for(long i = 0; i < maxIter; ++i)
std::cout << boost::format("%0.10f:%04d:%+g:%s:%p:%c:%%\n")
% 1.234 % 42 % 3.13 % "str" % (void*)1000 % (int)'X';
}
else
{
assert(0 && "speed test for which version?");
}
}
int main(int argc, char* argv[])
{
if(argc >= 2)
speedTest(argv[1]);
return 0;
}