Skip to content

Commit c4e4e17

Browse files
committed
Split out speed tests to make things cleaner
Less unnecessary macros? This must be a good thing :)
1 parent d664ff7 commit c4e4e17

File tree

3 files changed

+65
-63
lines changed

3 files changed

+65
-63
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tinyformat.html: README.rst
3737
rst2html README.rst > tinyformat.html
3838

3939
tinyformat_speed_test: tinyformat.h tinyformat_test.cpp
40-
$(CXX) $(CXXFLAGS) -O3 -DSPEED_TEST tinyformat_test.cpp -o tinyformat_speed_test
40+
$(CXX) $(CXXFLAGS) -O3 tinyformat_speed_test.cpp -o tinyformat_speed_test
4141

4242
clean:
4343
rm -f tinyformat_test_cxx98 tinyformat_test_cxx0x tinyformat_speed_test

tinyformat_speed_test.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#if defined(__linux__) && defined(__clang__)
2+
// Workaround for bug in gcc 4.4 standard library headers when compling with
3+
// clang in C++11 mode.
4+
namespace std { class type_info; }
5+
#endif
6+
7+
#include <boost/format.hpp>
8+
#include <iomanip>
9+
#include <stdio.h>
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[])
60+
{
61+
if(argc >= 2)
62+
speedTest(argv[1]);
63+
return 0;
64+
}

tinyformat_test.cpp

-62
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ namespace std { class type_info; }
99
#include <cfloat>
1010
#include <cstddef>
1111

12-
#ifdef SPEED_TEST
13-
#include <boost/format.hpp>
14-
#include <iomanip>
15-
#include <stdio.h>
16-
#endif
17-
18-
1912
// Throw instead of abort() so we can test error conditions.
2013
#define TINYFORMAT_ERROR(reason) \
2114
throw std::runtime_error(reason);
@@ -213,62 +206,7 @@ int unitTests()
213206
}
214207

215208

216-
#ifdef SPEED_TEST
217-
void speedTest(const std::string& which)
218-
{
219-
// Following is required so that we're not limited by per-character
220-
// buffering.
221-
std::ios_base::sync_with_stdio(false);
222-
const long maxIter = 2000000L;
223-
if(which == "printf")
224-
{
225-
// libc version
226-
for(long i = 0; i < maxIter; ++i)
227-
printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n",
228-
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
229-
}
230-
else if(which == "iostreams")
231-
{
232-
// Std iostreams version. What a mess!!
233-
for(long i = 0; i < maxIter; ++i)
234-
std::cout
235-
<< std::setprecision(10) << std::fixed << 1.234
236-
<< std::resetiosflags(std::ios::floatfield) << ":"
237-
<< std::setw(4) << std::setfill('0') << 42 << std::setfill(' ') << ":"
238-
<< std::setiosflags(std::ios::showpos) << 3.13 << std::resetiosflags(std::ios::showpos) << ":"
239-
<< "str" << ":"
240-
<< (void*)1000 << ":"
241-
<< 'X' << ":%\n";
242-
}
243-
else if(which == "tinyformat")
244-
{
245-
// tinyformat version.
246-
for(long i = 0; i < maxIter; ++i)
247-
tfm::printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n",
248-
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
249-
}
250-
else if(which == "boost")
251-
{
252-
// boost::format version
253-
for(long i = 0; i < maxIter; ++i)
254-
std::cout << boost::format("%0.10f:%04d:%+g:%s:%p:%c:%%\n")
255-
% 1.234 % 42 % 3.13 % "str" % (void*)1000 % (int)'X';
256-
}
257-
else
258-
{
259-
assert(0 && "speed test for which version?");
260-
}
261-
}
262-
#endif
263-
264-
265209
int main(int argc, char* argv[])
266210
{
267-
#ifdef SPEED_TEST
268-
if(argc >= 2)
269-
speedTest(argv[1]);
270-
return 0;
271-
#else
272211
return unitTests();
273-
#endif
274212
}

0 commit comments

Comments
 (0)