当前位置: 首页 > news >正文

micro benchmark 使用经验

文章目录

    • User Guide

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 experimentsstd::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;
}
http://www.lryc.cn/news/148353.html

相关文章:

  • nodejs发布静态https服务器
  • 国产系统下开发QT程序总结
  • 【Redis】redis入门+java操作redis
  • 无涯教程-Android - Spinner函数
  • 国标GB28181视频平台EasyGBS国标平台智能边缘计算网关关于小区电动车进电梯的应用方案设计
  • supervisorctl(-jar)启动配置设置NACOS不同命名空间
  • 如何解决 Out Of Memory 的问题
  • 代码随想录训练营二刷第九天 | 字符串结束
  • javaee spring jdbcTemplate的使用
  • qt day
  • Configurable Weather Parameters
  • MySQL 8 数据清洗总结
  • 设计模式第九讲:常见重构技巧 - 去除不必要的!=
  • 自动化安装系统问题记录
  • centos7 docker安装记录
  • 全网实时热点热榜事件API
  • 淘宝API接口:提高电商运营效率与用户体验的利器(淘宝API接口使用指南)
  • 智己 LS6 用实力和你卷,最强 800v ?
  • 深入探索C语言自定义类型:打造你的编程世界
  • Opencv基于文字检测去图片水印
  • jdbc235
  • 库仑定律和场强
  • Android安卓实战项目(13)---记账APP详细记录每天的收入和支出并且分类统计【生活助手类APP】强烈推荐自己也在用!!!(源码在文末)
  • 嵌入式开发之syslog和rsyslog构建日志记录
  • Jaeger的经典BUG原创
  • 四款简洁好看 自适应的APP下载单页源码
  • 【服务器】交换机带外管理和带内管理
  • Kotlin的内置函数 apply、let、run、with、also
  • 2023年人工景点行业研究报告
  • react轮播图