So I try to create simple speed bench with boost 1.47.0. But results like 1.423 are not detailed enough for me. I need more detailed ones. How to get tham? How to make boost::timer show microseconds?
bench code:
#include <iostream>
#include <boost/thread.hpp>
#include <map>
#include <boost/thread.hpp>
#include <boost/thread/locks.hpp>
#include开发者_如何学Python <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random.hpp>
#include <boost/timer.hpp>
#include <boost/progress.hpp>
class TestDs
{
public:
virtual bool containsKey(int key)=0;
virtual int get(int key)=0;
virtual int put(int key, int value)=0;
virtual int remove(int key)=0;
virtual int size()=0;
virtual const char* name()=0;
virtual void print()=0;
virtual void shutdown()=0;
};
class GeneralMap: public TestDs
{
private:
std::map<int,int> _ds;
mutable boost::mutex mut_;
public:
GeneralMap() {}
bool containsKey(int key) {
boost::mutex::scoped_lock lock(mut_);
if ( _ds.find(key) != _ds.end())
{
return true;
}
else
{
return false;
}
}
int get(int key) {
boost::mutex::scoped_lock lock(mut_);
return _ds[key];
}
int put(int key, int value) {
boost::mutex::scoped_lock lock(mut_);
_ds.insert(std::pair<int, int>(key,value));
return key;
}
int remove(int key) {
boost::mutex::scoped_lock lock(mut_);
return _ds.erase(key);
}
int size() {
boost::mutex::scoped_lock lock(mut_);
return _ds.size();
}
const char* name() {
return "StdMap";
}
void print() {}
void shutdown() {}
};
int n;
boost::shared_mutex tests;
boost::shared_mutex results;
boost::random::mt19937 rng;
boost::timer timerForCaptureFame;
GeneralMap Ds;
boost::progress_display *show_progress;
void test( int i)
{
boost::shared_lock<boost::shared_mutex> lock_r(results);
boost::shared_lock<boost::shared_mutex> lock(tests);
Ds.put(i, 0);
if (Ds.containsKey(i))
{
Ds.get(i);
}
Ds.remove(i);
++(*show_progress);
}
void result()
{
boost::upgrade_lock<boost::shared_mutex> lock(results);
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
std::cout << std::endl << "test of " << Ds.name() << " complite;" << std::endl << "test performed on " << n << " items" << std::endl << "test duration: " << timerForCaptureFame.elapsed() << std::endl;
}
void create_tests( int n)
{
boost::upgrade_lock<boost::shared_mutex> lock(tests);
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
boost::shared_lock<boost::shared_mutex> lock_r(results);
for(int i=0; i<n; i++)
{
boost::random::uniform_int_distribution<> ran = boost::random::uniform_int_distribution<>(1, n*10);
int x = ran(rng);
boost::thread worker(test, x);
}
boost::thread worker_r(result);
timerForCaptureFame.restart();
return;
}
int main()
{
n = 1000;
show_progress = new boost::progress_display(n);
create_tests(n);
std::cin.get();
return 0;
}
How to make boost::timer show microseconds?
Boost's timer library is dependant on the C standard library clock() function and CLOCKS_PER_SEC. That maximum resolution you can get depends on that, so I would investigate that first to see if your system supports microsecond resolution.
MSVC is set to CLOCKS_PER_SEC == 1000 so you wouldn't be able to get microsecond resolution if that's what you are compiling with. Other systems do support CLOCKS_PER_SECOND == 1000000, so you should consider setting fixed output format and setting a precision for doubles:
std::cout << std::fixed << std::precision(6) << timerForCaptureFame.elapsed();
See if this thread from the Boost mailing page helps.
And you could look at this Stackoverflow question
精彩评论