nanobench
Simple, fast, accurate single-header microbenchmarking functionality for C++11/14/17/20
Top Related Projects
A microbenchmark support library
GoogleTest - Google Testing and Mocking Framework
Quick Overview
Nanobench is a simple, fast, and accurate microbenchmarking library for C++. It provides a straightforward way to measure and compare the performance of small code snippets, functions, or algorithms with minimal overhead and high precision.
Pros
- Easy to use with a simple API
- Highly accurate measurements with low overhead
- Supports multiple output formats (console, JSON, CSV)
- Header-only library, easy to integrate into existing projects
Cons
- Limited to C++ projects
- May require some setup for more complex benchmarking scenarios
- Not as feature-rich as some larger benchmarking frameworks
- Documentation could be more comprehensive
Code Examples
- Basic benchmark:
#include <nanobench.h>
int main() {
ankerl::nanobench::Bench().run("example", []() {
// Code to benchmark
volatile int x = 1 + 1;
});
}
- Comparing multiple functions:
#include <nanobench.h>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v(1000);
ankerl::nanobench::Bench()
.run("std::sort", [&] {
std::sort(v.begin(), v.end());
})
.run("std::stable_sort", [&] {
std::stable_sort(v.begin(), v.end());
});
}
- Custom output:
#include <nanobench.h>
int main() {
ankerl::nanobench::Bench()
.output(nullptr) // Disable console output
.run("example", [] {
volatile int x = 1 + 1;
})
.render(ankerl::nanobench::templates::json(), std::cout);
}
Getting Started
To use Nanobench in your C++ project:
- Download the
nanobench.h
header file from the GitHub repository. - Include the header in your C++ file:
#include "nanobench.h"
- Create a benchmark:
int main() { ankerl::nanobench::Bench().run("my_benchmark", [] { // Your code to benchmark here }); return 0; }
- Compile and run your program to see the benchmark results.
Competitor Comparisons
A microbenchmark support library
Pros of benchmark
- More comprehensive feature set, including multithreaded benchmarking
- Wider adoption and community support
- Extensive documentation and examples
Cons of benchmark
- Larger and more complex, potentially overkill for simple use cases
- Requires more setup and configuration
Code Comparison
nanobench:
#include <nanobench.h>
ankerl::nanobench::Bench().run("my_function", [&] {
my_function();
});
benchmark:
#include <benchmark/benchmark.h>
static void BM_MyFunction(benchmark::State& state) {
for (auto _ : state) {
my_function();
}
}
BENCHMARK(BM_MyFunction);
Key Differences
- nanobench is header-only and easier to integrate
- benchmark offers more granular control over benchmark parameters
- nanobench provides built-in statistical analysis
- benchmark has better support for custom output formats
Use Cases
- nanobench: Quick, simple benchmarks with minimal setup
- benchmark: Complex benchmarking scenarios, especially in larger projects
Performance
Both libraries have low overhead, but nanobench may have a slight edge in simplicity and compile-time performance.
GoogleTest - Google Testing and Mocking Framework
Pros of Google Test
- Comprehensive testing framework with a wide range of features
- Extensive documentation and community support
- Supports various testing styles (e.g., fixture, parameterized, typed tests)
Cons of Google Test
- Larger codebase and potential overhead for simple projects
- Steeper learning curve for beginners
- May require more setup and configuration
Code Comparison
Google Test:
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(Factorial(0), 1);
}
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(Factorial(1), 1);
EXPECT_EQ(Factorial(2), 2);
EXPECT_EQ(Factorial(3), 6);
}
Nanobench:
ankerl::nanobench::Bench().run("factorial", [&] {
ankerl::nanobench::doNotOptimizeAway(Factorial(10));
});
Key Differences
- Google Test focuses on unit testing and assertions
- Nanobench specializes in micro-benchmarking and performance measurement
- Google Test provides a more extensive testing ecosystem
- Nanobench offers a simpler API for quick benchmarking tasks
Use Cases
- Choose Google Test for comprehensive unit testing and test-driven development
- Opt for Nanobench when primarily concerned with performance benchmarking and optimization
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ankerl::nanobench
ankerl::nanobench
is a platform independent microbenchmarking library for C++11/14/17/20.
#define ANKERL_NANOBENCH_IMPLEMENT
#include <nanobench.h>
int main() {
double d = 1.0;
ankerl::nanobench::Bench().run("some double ops", [&] {
d += 1.0 / d;
if (d > 5.0) {
d -= 5.0;
}
ankerl::nanobench::doNotOptimizeAway(d);
});
}
The whole executable runs for ~60ms and prints
| ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
| 7.52 | 132,948,239.79 | 1.1% | 6.65 | 24.07 | 0.276 | 1.00 | 8.9% | 0.00 | `some double ops`
Which github renders as
ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark |
---|---|---|---|---|---|---|---|---|---|
7.52 | 132,948,239.79 | 1.1% | 6.65 | 24.07 | 0.276 | 1.00 | 8.9% | 0.00 | some double ops |
The benchmarked code takes 7.52 nanoseconds to run, so ~133 million times per seconds. Measurements fluctuate by
1.1%. On average 6.65 instructions are executed in 24.07 CPU cycles, resulting in 0.276 instructions per
cycle. A single branch is in the code, which branch prediction missed in 8.9% of the cases. Total runtime of
the benchmark with the name some double ops
is 0.00, so just a few milliseconds.
Design Goals
- Ease of use: Simple & powerful API, fast compile times, easy to integrate anywhere.
- Fast: Get accurate results as fast as possible. nanobench is ~80 times faster than google benchmark.
- Accurate: Get deterministic, repeatable, and accurate results that you can make sound decisions on.
- Robust: Be robust against outliers, warn if results are not reliable.
Documentation
Extensive documentation is available.
More
- Code of Conduct - Contributor Covenant Code of Conduct
- I need a better logo. Currently I use a small bench. Nanobench. Ha ha.
Top Related Projects
A microbenchmark support library
GoogleTest - Google Testing and Mocking Framework
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot