Ticket #3218: perf.cpp

File perf.cpp, 2.5 KB (added by Yuri Goldfeld <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().

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 Time_t start, dur, prevDur;
28 string str;
29#define TIMER_START \
30 start = TimeNow()
31#define REPEAT(steps) \
32 for (unsigned int i = 0; i < 3000000; ++i) \
33 { \
34 steps; \
35 }
36#define TIMER_END(desc) \
37 dur = TimeNow() - start; \
38 cout << desc << "| " << dur << " msec.\n" << flush
39#define TIMER_END2(desc) \
40 prevDur = dur; \
41 dur = TimeNow() - start; \
42 cout << desc << "| " << dur << " msec.\n" << flush; \
43 cout << "Boost/hand ratio = " << (float(dur) / float(prevDur)) << "\n\n" << flush
44#define TEST(steps) \
45 TIMER_START; \
46 REPEAT(steps); \
47 TIMER_END(#steps);
48#define TEST2(steps) \
49 TIMER_START; \
50 REPEAT(steps); \
51 TIMER_END2(#steps);
52
53 TEST(string("abcdefgh") == string("abcdefgh"));
54 TEST2(equals(string("abcdefgh"), string("abcdefgh")));
55
56 TEST(StartsWith(string("abcdefghi"), "abcd"));
57 TEST2(starts_with(string("abcdefghi"), "abcd"));
58
59 TEST(StartsWithI(string("abcdefghi"), "aBcD"));
60 TEST2(istarts_with(string("abcdefghi"), "aBcD"));
61
62 TEST(EqualsI("abcdefghi", "aBcDeFgHi"));
63 TEST2(iequals("abcdefghi", "aBcDeFgHi"));
64
65 TEST(ToUpperCopy(string("aBcDeFgHi")));
66 TEST2(to_upper_copy(string("aBcDeFgHi")));
67
68 TEST(ToLowerCopy(string("aBcDeFgHi")));
69 TEST2(to_lower_copy(string("aBcDeFgHi")));
70
71 str = "aBcDeFgHi";
72 TEST(ToLower(str));
73 str = "aBcDeFgHi";
74 TEST2(to_lower(str));
75
76 str = "aBcDeFgHi";
77 TEST(ToUpper(str));
78 str = "aBcDeFgHi";
79 TEST2(to_upper(str));
80
81 return 0;
82}
83
84#ifdef UNIX32
85# include <sys/time.h>
86
87Time_t TimeNow()
88{
89 timeval tm;
90 gettimeofday(&tm, NULL);
91 return Time_t(tm.tv_sec) * 1000ULL + Time_t(tm.tv_usec) / 1000ULL;
92}
93
94#elif defined(WIN32)
95# include <Windows.h>
96
97Time_t TimeNow()
98{
99 LARGE_INTEGER countsPerSec;
100 BOOL res = QueryPerformanceFrequency(&countsPerSec);
101 assert(res);
102
103 LARGE_INTEGER count;
104 res = QueryPerformanceCounter(&count);
105 assert(res);
106
107 return count.QuadPart / (countsPerSec.QuadPart / 1000LL);
108}
109
110#else
111
112#error WIN32 or UNIX32 must be defined.
113
114#endif