micro benchmark 使用经验

文章目录

User Guide

  1. User Guide: https://github.com/google/benchmark/blob/main/docs/user_guide.md
#include <benchmark/benchmark.h>
#include <chrono>
#include <thread>

void BM_DemoSleep(benchmark::State& state) {
  for (auto _ : state){
    //待测试的代码
  }
}
BENCHMARK(BM_DemoSleep); // 注册要测试的函数对象

BENCHMARK_MAIN(); // main函数,运行benchmark初始化和执行

kNanosecond, kMicrosecond, kMillisecond, kSecond 千纳秒, 千微秒, 千毫秒, 千秒。

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <fstream>
#include <ctime>
#include <stdio.h>
#include <math.h>
#include <iomanip>
#include <algorithm>
#include <chrono>
#include <thread>
#include <random>
#include <unordered_set>
#include "boost/unordered_set.hpp"

#include <benchmark/benchmark.h>

using namespace std;

vector<int> datas;
vector<int> findDatas;

void BM_Demo_1(benchmark::State& state) 
{
  //auto Parameters = state.range(0);
  //cout<<Parameters<<endl;
  unordered_set<int> data;
  for (auto _ : state)
  {
    
    for(unsigned int i = 0; i < datas.size(); ++i) {
      data.insert(datas[i]);
    }
    for(unsigned int i = 0; i < findDatas.size(); ++i) {
       std::unordered_set<int>::const_iterator got = data.find(findDatas[i]);
    }

    //state.PauseTiming(); // pause timing
    //state.ResumeTiming(); // resume timing
  }
}

void BM_Demo_2(benchmark::State& state) 
{
  boost::unordered_set<int> data;
  for (auto _ : state)
  {

    for(unsigned int i = 0; i < datas.size(); ++i) {
      data.insert(datas[i]);
    } 

    for(unsigned int i = 0; i < findDatas.size(); ++i) {
      boost::unordered_set<int>::const_iterator got = data.find(findDatas[i]);
    }

  }
}

int main(int argc, char** argv) 
{
  ::benchmark::Initialize(&argc, argv); 
  if (::benchmark::ReportUnrecognizedArguments(argc, argv)) {
    return 1;
  }

  const int nrolls=10000;  // number of experiments
  std::default_random_engine generator;   
  std::uniform_int_distribution<int> distribution(0,5000);

  for (int i=0; i<nrolls; ++i) 
  {     
    int number = distribution(generator); 
    datas.push_back(number);
  }

  for (int i=0; i<nrolls*0.01; ++i) 
  {     
     int number = distribution(generator); 
    findDatas.push_back(number);
  }
  // The Arg method for passing parameters using the BENCHMARK macro to generate objects  
  // The parameters passed in will be stored inside the state object and obtained through the range method. 
  // The parameter 0 required during the call corresponds to the first parameter
  //BENCHMARK(BM_Demo_1)->Arg(1);

  // Passing more Parameters
  //BENCHMARK(BM_Demo_1)->Args({10, 100});

  //BENCHMARK(BM_Demo_1)->Arg(10);
  //BENCHMARK(BM_Demo_1)->Arg(100);
    
  //BENCHMARK(BM_Demo_1)->RangeMultiplier(10)->Range(10, 1000);

  // multithreading
  // ->Threads(int t)
  // ->ThreadRange(int min_threads, int max_threads)
  // ->DenseThreadRange(int min_threads, int max_threads, int stride = 1);
  //::benchmark::RegisterBenchmark("BM_Demo_1", &BM_Demo_1)->Threads(10);
  
  
  // Repeat the iteration for 10 times, which means that the for (auto_: state) {} loop will iterate 10 times; Repeat the call 3 times
  //::benchmark::RegisterBenchmark("BM_Demo_1", &BM_Demo_1)->Iterations(10)->Repetitions(3)->Unit(benchmark::kMillisecond);


  // Set the display time unit : kNanosecond, kMicrosecond, kMillisecond, kSecond.
  //::benchmark::RegisterBenchmark("BM_Demo_1", &BM_Demo_1)->Unit(benchmark::kNanosecond);


  //Statistical analysis results will calculate the results of each time, and then output the analysis results: 
  // mean, median, stddev: standard deviation, cv: standard deviation/mean. 
  // Customized analysis results, such as minimum and maximum values
  /*
  ::benchmark::RegisterBenchmark("BM_Demo_1", &BM_Demo_1)
  ->ComputeStatistics("max", [](const std::vector<double>& v)->double{
    return *std::max_element(v.begin(), v.end());
  }, benchmark::kTime)
  ->ComputeStatistics("min", [](const std::vector<double>& v)->double{
    return *std::min_element(v.begin(), v.end());
  }, benchmark::kTime);
  */

    //::benchmark::RegisterBenchmark("BM_Demo_1", &BM_Demo_1)->Iterations(100000)->Repetitions(1000)->Unit(benchmark::kNanosecond);
  //::benchmark::RegisterBenchmark("BM_Demo_2", &BM_Demo_2)->Iterations(100000)->Repetitions(1000)->Unit(benchmark::kNanosecond);

  ::benchmark::RegisterBenchmark("BM_Demo_1", &BM_Demo_1)->Unit(benchmark::kNanosecond);
  ::benchmark::RegisterBenchmark("BM_Demo_2", &BM_Demo_2)->Unit(benchmark::kNanosecond);

  
  ::benchmark::RunSpecifiedBenchmarks(); 
  ::benchmark::Shutdown(); 
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值