基于 boost 1.82…

timer 计时器

过去老的 boost/timer.hpp 已经废弃了,目前推荐使用的是 boost/timer/timer.hpp, 主要包括下面了2个类

classdetail
boost::timer::cpu_timer计时器
boost::timer::auto_cpu_timer计时器,基于cpu_timer实现,在析构的时候输出耗时
struct cpu_times {
    nanosecond_type wall;    // 挂钟时间
    nanosecond_type user;    // 用户时间
    nanosecond_type system;  // 系统时间

    void clear() {wall = user = system = 0LL; }
};

默认的输出格式为下:

“%w s wall, %u s user + %s s system = %t s CPU (%p%)\n”

formatmeaning
%wtimes.wall
%utimes.user
%stimes.system
%ttimes.user + times.system
%pThe percentage of times.wall represented by times.user + times.system

来个简单的例子:

#include <boost/timer/timer.hpp>
#include <cmath>
#include <iostream>

using namespace std;
using namespace boost;

int main() {
    timer::cpu_timer      t;
    timer::auto_cpu_timer auto_timer(6, "%ws real time\n");

    for (long i = 0; i < 100000000; ++i)
        auto _ = sqrt(i * i);  // spend some time

    cout << t.format(2, "%us user + %ss system = %ts(%p%)") << endl;

    t.start();
    for (long i = 0; i < 100000000; ++i)
        auto _ = sqrt(i * i);  // spend some time
    cout << t.format(2, "%us user + %ss system = %ts(%p%)") << endl;

    return 0;
}

split

stl缺少个split, 好难受啊
头文件为 boost/algorithm/string/split.hpp

#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
#include <set>

int main(int argc, char *argv[]) {
    std::string str = "123,,345,qwe;adq,345";
    std::set<std::string> st;
    boost::split(st, str, boost::is_any_of(",; "), boost::token_compress_on);
    for_each(st.begin(), st.end(), [](const std::string& x) {std::cout << "[" << x << "]\n";});

    return 0;
}

默认为 token_compress_off: 表示遇见多个token时候,不合并成一个token, 这时候,,分割后,会多一个空string,表示2个逗号中间的空string