std.benchmark

Members

Aliases

AutoStart
alias AutoStart = std.datetime.AutoStart
ComparingBenchmarkResult
alias ComparingBenchmarkResult = std.datetime.ComparingBenchmarkResult
StopWatch
alias StopWatch = std.datetime.StopWatch
comparingBenchmark
alias comparingBenchmark = std.datetime.comparingBenchmark
measureTime
alias measureTime = std.datetime.measureTime

Aliases from std.datetime.

Functions

benchmark
void benchmark(BenchmarkResult[] results, string moduleName)

Benchmarks functions and appends the results to the results parameter. This function is used by printBenchmarks, and the funs parameter has the same requirements as for that function.

benchmarkResume
void benchmarkResume()

Suspends and resumes the current benchmark, respectively. This is useful if the benchmark needs to set things up before performing the measurement.

benchmarkSuspend
void benchmarkSuspend()

Suspends and resumes the current benchmark, respectively. This is useful if the benchmark needs to set things up before performing the measurement.

printBenchmarks
void printBenchmarks(File target)

Benchmarks one or more functions for speed assessment and comparison, and prints results as formatted text. A baseline timing that accounts for benchmarking overheads is kept along with the results and automatically deducted from all timings. A timing indistinguishable from the baseline looping overhead appears with a run time of zero and indicates a function that does too little work to be timed.

printResults
void printResults(BenchmarkResult[] data, File target)

Prints benchmark results as described with printBenchmarks. This is useful if benchmark postprocessing is desired before printing.

runBenchmarks
void runBenchmarks(BenchmarkResult[] results)

Performs all benchmarks previously scheduled with scheduleForBenchmarking, application-wide. Usually not called directly, but instead as support for printBenchmarks. User code may be interested in using this function directly for e.g. results formatting.

scheduleForBenchmarking
string scheduleForBenchmarking()

The examples shown so far feature simple, ad-hoc benchmarks, but std.benchmark allows for superior automation and systematic use aimed at large projects.

Structs

BenchmarkResult
struct BenchmarkResult

Result of one function's benchmark.

Meta

Source

See Source File
std/benchmark.d

Synopsis:

1 module nxt.module_one;
2 import std.benchmark, std.file;
3 
4 void benchmark_fileWrite()
5 {
6     std.file.write("/tmp/deleteme", "hello, world!");
7 }
8 
9 void benchmark_fileRead()
10 {
11     std.file.read("/tmp/deleteme");
12 }
13 
14 mixin(scheduleForBenchmarking);
15 
16 void main()
17 {
18     printBenchmarks();
19 }

The code above prints:

===============================================================================
module_one                                             relative ns/iter  iter/s
===============================================================================
fileWrite                                                        144.2K    6.9K
fileRead                                                          27.1K   36.9K
===============================================================================