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 container append operations by benchmarking RWTValOrderedVector<T,A>::append() versus RWTValDlist<T,A>::append().
#include <cstdlib>
#include <iostream>
#include <rw/bench.h>
#include <rw/tvdlist.h>
#include <rw/tvordvec.h>
template <typename C>
class TimeAppend :
public RWBench {
public:
TimeAppend(
const RWCString& name) : name_(name) {}
virtual void doLoop(unsigned long);
virtual void idleLoop(unsigned long);
virtual void what(std::ostream& s) const {
s << name_ << "::append() test: \n";
}
private:
};
template <typename C>
void TimeAppend<C>::doLoop(unsigned long n) {
C container;
for (unsigned long i = 0; i < n; ++i) {
container.append(i);
}
}
template <typename C>
void TimeAppend<C>::idleLoop(unsigned long n) {
C container;
for (unsigned long i = 0; i < n; ++i) {
}
}
int main(int argc, char** argv) {
std::cout << "Testing container append...\n";
TimeAppend<RWTValOrderedVector<unsigned long> > vec(
"RWTValOrderedVector<unsigned long>");
vec.parse(argc, argv);
vec.go();
vec.report(std::cout);
TimeAppend<RWTValDlist<unsigned long> > list(
"RWTValDlist<unsigned long>");
list.parse(argc, argv);
list.go();
list.report(std::cout);
return 0;
}
Abstract base class to facilitate automatic benchmarking of some piece of code.
Definition bench.h:160
Offers powerful and convenient facilities for manipulating strings.
Definition stdcstring.h:826
Program Output:
Testing container append...
Microsoft C/C++
RWTValOrderedVector<unsigned long>::append() test:
Iterations: 4071
Inner loop operations: 1000
Total operations: 4.071e+006
Elapsed (user) time: 5.00763
Kilo-operations per second: 812.959
Microsoft C/C++
RWTValDlist<unsigned long>::append() test:
Iterations: 4066
Inner loop operations: 1000
Total operations: 4.066e+006
Elapsed (user) time: 5.00763
Kilo-operations per second: 811.961