Abstract base class to facilitate automatic benchmarking of some piece of code.
More...
This is an abstract class that can automate the process of benchmarking a piece of code. To use it, derive a class from RWBench, including a definition for the virtual function doLoop(unsigned long N). This function should perform N operations of the type that you are trying to benchmark. RWBench will call doLoop() over and over again until a preset amount of time has elapsed. It will then sum the total number of operations performed.
To run, construct an instance of your derived class and then call go(). Then call report() to get a standard summary. For many compilers, this summary will automatically include the compiler type and memory model. You can call ops(), outerLoops(), etc. for more detail.
If you wish to correct for overhead, then provide an idleLoop() function which should do all non-benchmark-related calculations.
Synopsis
#include <rw/bench.h>
(Abstract base class)
Persistence
None
Example
This example compares string parsing operations by benchmarking a brute force approach versus using RWCString.
#include <cstdlib>
#include <iostream>
#include <rw/bench.h>
#include <rw/cstring.h>
#include <rw/ctoken.h>
#include <rw/regexp.h>
static const char* g_string =
"A multi-character string with lots of words "
"in it to be parsed out and searched for.";
{
TestBrute() { }
virtual void doLoop(
unsigned long);
virtual void what (std::ostream& s)
const {
s << "Brute force string search: \n";
}
};
{
public:
TestRW() { }
virtual void doLoop(
unsigned long);
virtual void what (std::ostream& s)
const {
s << "Rogue Wave search: \n";
}
};
void TestBrute::doLoop(unsigned long n)
{
while (n--) {
if((token = (*tokener)()).isNull()) {
delete tokener;
token = (*tokener)();
}
size_t j = 0;
for(
size_t i = 0; i < s.length() && j != token.
length(); ++i)
for (j = 0; (j < token.
length ()) && (s [i + j] == token [j]); ++j) {
}
}
delete tokener;
}
void TestRW::doLoop (unsigned long n)
{
while(n--) {
if((token = (*tokener)()).isNull()) {
delete tokener;
token = (*tokener)();
}
result = s(re);
}
delete tokener;
}
void TestBrute::idleLoop(unsigned long n)
{
while(n--) {
if((token = (*tokener)()).isNull()) {
delete tokener;
token = (*tokener)();
}
}
delete tokener;
}
void TestRW::idleLoop(unsigned long n)
{
while(n--){
if((token = (*tokener)()).isNull()) {
delete tokener;
token = (*tokener)();
}
}
delete tokener;
}
int main (int argc, char** argv)
{
std::cout << "Testing string \"" << g_string << "\".\n";
TestBrute other;
other.parse(argc, argv);
other.go();
other.report(std::cout);
TestRW rw;
rw.parse(argc, argv);
rw.go();
rw.report(std::cout);
return 0;
}
Program Output:
Testing string
A multi-character string with lots of words in it to be parsed out and searched for.
Microsoft C/C++
Brute force string search:
Iterations: 60
Inner loop operations: 1000
Total operations: 60000
Elapsed (user)
time: 4.39632
Kilo-operations per second: 13.6478
Microsoft C/C++
Rogue Wave search:
Iterations: 138
Inner loop operations: 1000
Total operations: 138000
Elapsed (user)
time: 2.11304
Kilo-operations per second: 65.3088