Ticket #3218: perf.2.cpp

File perf.2.cpp, 2.6 KB (added by yuri_goldfeld@…, 13 years ago)

The driver program that includes Boost string_algo and the attached StrUtils?.hpp. It has a crude timer mechanism and a few performance tests in main(). In Windows, now prints whether VC++ checked iterators are being used.

Line 
1#include "StrUtils.hpp"
2#include <boost/algorithm/string.hpp>
3
4#include <iostream>
5#include <string>
6#include <cassert>
7
8using std::string;
9using std::cout;
10using std::flush;
11
12using boost::algorithm::starts_with;
13using boost::algorithm::istarts_with;
14using boost::algorithm::iequals;
15using boost::algorithm::to_lower_copy;
16using boost::algorithm::to_upper_copy;
17using boost::algorithm::to_lower;
18using boost::algorithm::to_upper;
19using boost::algorithm::equals;
20
21typedef unsigned long long Time_t;
22
23Time_t TimeNow();
24
25int main(int argc, const char** argv)
26{
27#ifdef WIN32
28 cout << "Windows build: _SECURE_SCL = " << _SECURE_SCL << "\n" << flush;
29#endif
30
31 Time_t start, dur, prevDur;
32 string str;
33#define TIMER_START \
34 start = TimeNow()
35#define REPEAT(steps) \
36 for (unsigned int i = 0; i < 3000000; ++i) \
37 { \
38 steps; \
39 }
40#define TIMER_END(desc) \
41 dur = TimeNow() - start; \
42 cout << desc << "| " << dur << " msec.\n" << flush
43#define TIMER_END2(desc) \
44 prevDur = dur; \
45 dur = TimeNow() - start; \
46 cout << desc << "| " << dur << " msec.\n" << flush; \
47 cout << "Boost/hand ratio = " << (float(dur) / float(prevDur)) << "\n\n" << flush
48#define TEST(steps) \
49 TIMER_START; \
50 REPEAT(steps); \
51 TIMER_END(#steps);
52#define TEST2(steps) \
53 TIMER_START; \
54 REPEAT(steps); \
55 TIMER_END2(#steps);
56
57 TEST(string("abcdefgh") == string("abcdefgh"));
58 TEST2(equals(string("abcdefgh"), string("abcdefgh")));
59
60 TEST(StartsWith(string("abcdefghi"), "abcd"));
61 TEST2(starts_with(string("abcdefghi"), "abcd"));
62
63 TEST(StartsWithI(string("abcdefghi"), "aBcD"));
64 TEST2(istarts_with(string("abcdefghi"), "aBcD"));
65
66 TEST(EqualsI("abcdefghi", "aBcDeFgHi"));
67 TEST2(iequals("abcdefghi", "aBcDeFgHi"));
68
69 TEST(ToUpperCopy(string("aBcDeFgHi")));
70 TEST2(to_upper_copy(string("aBcDeFgHi")));
71
72 TEST(ToLowerCopy(string("aBcDeFgHi")));
73 TEST2(to_lower_copy(string("aBcDeFgHi")));
74
75 str = "aBcDeFgHi";
76 TEST(ToLower(str));
77 str = "aBcDeFgHi";
78 TEST2(to_lower(str));
79
80 str = "aBcDeFgHi";
81 TEST(ToUpper(str));
82 str = "aBcDeFgHi";
83 TEST2(to_upper(str));
84
85 return 0;
86}
87
88#ifdef UNIX32
89# include <sys/time.h>
90
91Time_t TimeNow()
92{
93 timeval tm;
94 gettimeofday(&tm, NULL);
95 return Time_t(tm.tv_sec) * 1000ULL + Time_t(tm.tv_usec) / 1000ULL;
96}
97
98#elif defined(WIN32)
99# include <Windows.h>
100
101Time_t TimeNow()
102{
103 LARGE_INTEGER countsPerSec;
104 BOOL res = QueryPerformanceFrequency(&countsPerSec);
105 assert(res);
106
107 LARGE_INTEGER count;
108 res = QueryPerformanceCounter(&count);
109 assert(res);
110
111 return count.QuadPart / (countsPerSec.QuadPart / 1000LL);
112}
113
114#else
115
116# error WIN32 or UNIX32 must be defined.
117
118#endif