Wall-clock + cycle-accurate benchmarking for performance profiling.
More...
#include <chrono>
#include <functional>
#include <iostream>
#include <string>
Go to the source code of this file.
|
void | benchmark (const std::string &name, int trials, std::function< int()> func) |
| Benchmark wrapper that logs wall time and CPU cycles.
|
|
Wall-clock + cycle-accurate benchmarking for performance profiling.
Wraps a simulation function and logs:
- Wall time in nanoseconds + seconds
- CPU cycles (via rdtsc or cntvct_el0)
- π estimate from number of hits
Features
- Cross-platform CPU cycle counting (x86 + ARM)
- High-resolution
std::chrono
timer
- Printable results for logging or scripting
Example
});
void benchmark(const std::string &name, int trials, std::function< int()> func)
Benchmark wrapper that logs wall time and CPU cycles.
int monteCarloPI_SEQUENTIAl(int numberOfTrials)
Estimates π using sequential dart throwing.
Definition in file benchmark.hpp.
◆ benchmark()
void benchmark |
( |
const std::string & | name, |
|
|
int | trials, |
|
|
std::function< int()> | func ) |
Benchmark wrapper that logs wall time and CPU cycles.
Times the execution of a function using both std::chrono. Outputs hit count and estimate of π. duration in seconds and nanoseconds
- Parameters
-
name | Name of the benchmark (e.g., "SIMD") |
trials | Total number of Monte Carlo trials |
func | Function that returns number of hits |
Definition at line 44 of file benchmark.hpp.
44 {
45 auto start = std::chrono::high_resolution_clock::now();
46
47 int hits = func();
48
49 auto end = std::chrono::high_resolution_clock::now();
50
51 double piEstimate = 4.0 * hits /
trials;
52 auto elapsed_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
53
54 std::cout << name << ":\n"
55 <<
" Trials: " <<
trials <<
"\n"
56 << " Hits: " << hits << "\n"
57 << " Estimate: " << piEstimate << "\n"
58 << " Time: " << (elapsed_ns / 1e9) << "s (" << elapsed_ns << " ns)\n";
59}