micro benchmark 使用经验

文章目录

    • User Guide

User Guide

  1. User Guide: https://github.com/google/benchmark/blob/main/docs/user_guide.md
#include 
#include 
#include 

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

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

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "boost/unordered_set.hpp"

#include 

using namespace std;

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

void BM_Demo_1(benchmark::State& state) 
{
  //auto Parameters = state.range(0);
  //cout<
  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& v)->double{
    return *std::max_element(v.begin(), v.end());
  }, benchmark::kTime)
  ->ComputeStatistics("min", [](const std::vector& 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;
}

你可能感兴趣的:(C++,c++,算法,性能优化)