|
| 1 | +#!/usr/bin/env julia |
| 2 | + |
| 3 | +import BenchmarkTools |
| 4 | + |
| 5 | +# Benchmark variables: |
| 6 | +name = "if-else"; |
| 7 | +repeats = 3; |
| 8 | + |
| 9 | +""" |
| 10 | + print_version() |
| 11 | +
|
| 12 | +Prints the TAP version. |
| 13 | +
|
| 14 | +# Examples |
| 15 | +
|
| 16 | +``` julia |
| 17 | +julia> print_version() |
| 18 | +``` |
| 19 | +""" |
| 20 | +function print_version() |
| 21 | + @printf( "TAP version 13\n" ); |
| 22 | +end |
| 23 | + |
| 24 | +""" |
| 25 | + print_summary( total, passing ) |
| 26 | +
|
| 27 | +Print the benchmark summary. |
| 28 | +
|
| 29 | +# Arguments |
| 30 | +
|
| 31 | +* `total`: total number of tests |
| 32 | +* `passing`: number of passing tests |
| 33 | +
|
| 34 | +# Examples |
| 35 | +
|
| 36 | +``` julia |
| 37 | +julia> print_summary( 3, 3 ) |
| 38 | +``` |
| 39 | +""" |
| 40 | +function print_summary( total, passing ) |
| 41 | + @printf( "#\n" ); |
| 42 | + @printf( "1..%d\n", total ); # TAP plan |
| 43 | + @printf( "# total %d\n", total ); |
| 44 | + @printf( "# pass %d\n", passing ); |
| 45 | + @printf( "#\n" ); |
| 46 | + @printf( "# ok\n" ); |
| 47 | +end |
| 48 | + |
| 49 | +""" |
| 50 | + print_results( iterations, elapsed ) |
| 51 | +
|
| 52 | +Print benchmark results. |
| 53 | +
|
| 54 | +# Arguments |
| 55 | +
|
| 56 | +* `iterations`: number of iterations |
| 57 | +* `elapsed`: elapsed time (in seconds) |
| 58 | +
|
| 59 | +# Examples |
| 60 | +
|
| 61 | +``` julia |
| 62 | +julia> print_results( 1000000, 0.131009101868 ) |
| 63 | +``` |
| 64 | +""" |
| 65 | +function print_results( iterations, elapsed ) |
| 66 | + rate = iterations / elapsed |
| 67 | + |
| 68 | + @printf( " ---\n" ); |
| 69 | + @printf( " iterations: %d\n", iterations ); |
| 70 | + @printf( " elapsed: %0.9f\n", elapsed ); |
| 71 | + @printf( " rate: %0.9f\n", rate ); |
| 72 | + @printf( " ...\n" ); |
| 73 | +end |
| 74 | + |
| 75 | +""" |
| 76 | + benchmark() |
| 77 | +
|
| 78 | +Run a benchmark. |
| 79 | +
|
| 80 | +# Notes |
| 81 | +
|
| 82 | +* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. |
| 83 | +* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. |
| 84 | +* The elapsed time is in seconds. |
| 85 | +
|
| 86 | +# Examples |
| 87 | +
|
| 88 | +``` julia |
| 89 | +julia> benchmark(); |
| 90 | +``` |
| 91 | +""" |
| 92 | +function benchmark() |
| 93 | + t = BenchmarkTools.@benchmark ifelse( rand() > 0.5, 1.0, -1.0 ) samples=1e6 |
| 94 | + |
| 95 | + # Compute the total "elapsed" time and convert from nanoseconds to seconds: |
| 96 | + s = sum( t.times ) / 1.0e9; |
| 97 | + |
| 98 | + # Determine the number of "iterations": |
| 99 | + iter = length( t.times ); |
| 100 | + |
| 101 | + # Return the results: |
| 102 | + [ iter, s ]; |
| 103 | +end |
| 104 | + |
| 105 | +""" |
| 106 | + main() |
| 107 | +
|
| 108 | +Run benchmarks. |
| 109 | +
|
| 110 | +# Examples |
| 111 | +
|
| 112 | +``` julia |
| 113 | +julia> main(); |
| 114 | +``` |
| 115 | +""" |
| 116 | +function main() |
| 117 | + print_version(); |
| 118 | + for i in 1:3 |
| 119 | + @printf( "# julia::%s\n", name ); |
| 120 | + results = benchmark(); |
| 121 | + print_results( results[ 1 ], results[ 2 ] ); |
| 122 | + @printf( "ok %d benchmark finished\n", i ); |
| 123 | + end |
| 124 | + print_summary( repeats, repeats ); |
| 125 | +end |
| 126 | + |
| 127 | +main(); |
0 commit comments