Boost计时器 - 如何设置细节级别,我想要微秒?

时间:2011-08-27 20:56:19

标签: c++ boost timer

所以我尝试使用boost 1.47.0创建简单的速度平台。但像1​​.423这样的结果对我来说还不够详细。我需要更详细的。怎么去tham​​?如何使boost :: timer显示微秒?

替补代码:

#include <iostream>
#include <boost/thread.hpp>
#include <map>
#include <boost/thread.hpp>
#include <boost/thread/locks.hpp>
#include <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;
}

2 个答案:

答案 0 :(得分:2)

  

如何使boost :: timer显示微秒?

Boost的定时器库依赖于C标准库clock()函数和CLOCKS_PER_SEC。你可以获得的最高分辨率取决于那个,所以我先调查一下你的系统是否支持微秒分辨率。

MSVC设置为CLOCKS_PER_SEC == 1000,因此如果您正在编译,则无法获得微秒分辨率。其他系统支持CLOCKS_PER_SECOND == 1000000,因此您应该考虑设置固定输出格式并设置双精度:

std::cout << std::fixed << std::precision(6) << timerForCaptureFame.elapsed();

答案 1 :(得分:1)

查看Boost邮件页面中的thread是否有帮助 您可以查看此Stackoverflow问题