Home > Backend Development > C++ > body text

How to benchmark C++ function performance?

王林
Release: 2024-04-19 09:00:02
Original
771 people have browsed it

To benchmark a C function, you can take the following steps: Use a timing tool (such as the std::chrono library) to measure the execution time. Write a benchmark function that executes code and returns the execution time. Leverage the benchmark library for advanced features such as statistics collection and comparison.

如何对 C++ 函数性能进行基准测试?

How to benchmark C function performance

Benchmarking is an important technique for measuring code performance and comparing different implementations . In C, we can benchmark function performance through the following methods:

1. Using timing tools

C providesstd::chrono Library containing classes for measuring time. We can use std::chrono::high_resolution_clock to obtain high-precision timing:

#include <chrono>

using namespace std::chrono;

auto start = high_resolution_clock::now();
// 待测试代码
auto end = high_resolution_clock::now();
Copy after login

2. Write a benchmark function

Write a function to Execute the code to be tested and return the execution time:

#include <chrono>

using namespace std::chrono;

double benchmark(int n) {
  auto start = high_resolution_clock::now();
  // 待测试代码
  auto end = high_resolution_clock::now();
  return duration_cast<duration<double>>(end - start).count();
}
Copy after login

3. Using a benchmark library

There are also various C benchmark libraries available that provide more Advanced features such as statistics collection and comparison. Here are some popular libraries:

  • [benchmark](https://github.com/google/benchmark)
  • [boost::benchmark](https://www .boost.org/doc/libs/1_65_1/libs/benchmark/doc/html/index.html)
  • [google-benchmark](https://github.com/google/benchmark)
  • [Catch2](https://github.com/catchorg/Catch2)

Practical case:

Suppose we want to benchmark a search Functionfind_element():

#include <chrono>
#include <vector>

using namespace std::chrono;

double find_element_benchmark(size_t n) {
  // 生成一个包含 n 个元素的数组
  std::vector<int> arr(n, 0);
  
  // 查找一个不存在的元素
  auto start = high_resolution_clock::now();
  auto pos = std::find(arr.begin(), arr.end(), -1);
  auto end = high_resolution_clock::now();
  if (pos != arr.end()) return -1;  // 仅在元素找到时返回 -1

  return duration_cast<duration<double>>(end - start).count();
}

int main() {
  // 多次测试不同数组大小
  for (size_t n = 1000; n <= 1000000; n *= 10) {
    // 运行基准测试
    double time = find_element_benchmark(n);
    
    // 打印结果
    std::cout << "数组大小: " << n << "\t执行时间: " << time << " 秒" << std::endl;
  }

  return 0;
}
Copy after login
given an element in an array

The above is the detailed content of How to benchmark C++ function performance?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template