Skip to content

Instantly share code, notes, and snippets.

@itsgreggreg
Last active January 18, 2021 09:49
Show Gist options
  • Save itsgreggreg/9e1f86e6b5f5b7e869ab89246f543cf3 to your computer and use it in GitHub Desktop.
Save itsgreggreg/9e1f86e6b5f5b7e869ab89246f543cf3 to your computer and use it in GitHub Desktop.
-module(bench_inline_type).
-export([non_inline_worst_case/1,
bench_non_inline_worst_case/2,
inline_worst_case/1,
bench_inline_worst_case/2,
non_inline_function_head_match/1,
bench_non_inline_function_head_match/2,
inline_function_head_match/1,
bench_inline_function_head_match/2,
non_inline_best_case/1,
bench_non_inline_best_case/2,
inline_best_case/1,
bench_inline_best_case/2
]).
%
% Worst Case
%
non_inline_worst_case(_) ->
lists:seq(1, 10000).
bench_non_inline_worst_case(T, _) ->
lists:map(fun(X) -> do_non_inline_worst_case({id, X}, {id, X}) end, T).
do_non_inline_worst_case(L, R) ->
case {L, R} of
{{id, L@1}, {id, R@1}} ->
{id, L@1 + R@1}
end.
inline_worst_case(_) ->
lists:seq(1, 10000).
bench_inline_worst_case(T, _) ->
lists:map(fun(X) -> do_inline_worst_case(X, X) end, T).
do_inline_worst_case(L, R) ->
case {L, R} of
{L@1, R@1} ->
L@1 + R@1
end.
%
% function head pattern match
%
non_inline_function_head_match(_) ->
lists:seq(1, 10000).
bench_non_inline_function_head_match(T, _) ->
lists:map(fun(X) -> do_non_inline_function_head_match({id, X}, {id, X}) end, T).
do_non_inline_function_head_match({id, L}, {id, R}) ->
{id, L + R}.
inline_function_head_match(_) ->
lists:seq(1, 10000).
bench_inline_function_head_match(T, _) ->
lists:map(fun(X) -> do_inline_function_head_match(X, X) end, T).
do_inline_function_head_match(L, R) ->
L + R.
%
% Best Case: function head pattern match and guard
%
non_inline_best_case(_) ->
lists:seq(1, 10000).
bench_non_inline_best_case(T, _) ->
lists:map(fun(X) -> do_non_inline_best_case({id, X}, {id, X}) end, T).
do_non_inline_best_case({id, L}, {id, R}) when is_integer(L) andalso is_integer(R) ->
{id, L + R}.
inline_best_case(_) ->
lists:seq(1, 10000).
bench_inline_best_case(T, _) ->
lists:map(fun(X) -> do_inline_best_case(X, X) end, T).
do_inline_best_case(L, R) when is_integer(L) andalso is_integer(R) ->
L + R.

To test this yourself

Looks like inline is about %35 faster, which at first blush seems significant, but we're talking 10_000 additions with a difference of ~250μs. That's a difference of .000000025s per call. I imagine the difference will be vanishingly smaller for operations more complex than an unwrapping and an addition.

If it takes ~250μs to do 10,000 unwrappings, you'd have to do 40,000,000 unwrappings for there to be a second of difference between inline or not. At which point in the simple case of addition on my machine it would take 3 seconds and change to do 40million additions rather than 2 seconds and change.

Ignore the (+ ... and (- ... columns in the output. That's compared to previous runs on my machine of the same function, not functions compared against eachother.

===> Analyzing applications...
===> Compiling gleam_bench
===> Calling deprecated rebar_erlc_compiler compiler module. This module has been replaced by rebar_compiler and rebar_compiler_erl, but will remain available.
===> Testing bench_inline_type:bench_non_inline_worst_case()
===> Stats for wall_time
Min: 745.25μs (- 98.39μs / 11.7%)
25 percentile: 759.67μs (- 94.79μs / 11.1%)
Median: 762.83μs (- 94.53μs / 11.0%)
75 percentile: 767.08μs (- 94.92μs / 11.0%)
Max: 953.25μs (- 91.66μs / 8.8%)
Outliers: Lo: 12; Hi: 110; Sum: 122
Outlier variance: 0.613706 (severe)
> Bootstrapped
Mean: 766.68μs (- 95.26μs / 11.1%)
Std deviation: 15.92μs
> Relative
Difference at 95.0 confidence
-95.26μs ± 1431.00ns
-11.05% ± 0.17%
(Student's t, pooled s = 1.63250e+4
===> Outlier variance is too high! Benchmark result might be non-representative. Try to repeat the benchmarks in more calm environment (no heavy background OS tasks) or run benchmark for a longer time
===> Testing bench_inline_type:bench_inline_worst_case()
===> Stats for wall_time
Min: 526.00μs (- 88.25μs / 14.4%)
25 percentile: 533.22μs (- 87.90μs / 14.2%)
Median: 538.11μs (- 85.89μs / 13.8%)
75 percentile: 540.89μs (- 86.67μs / 13.8%)
Max: 584.61μs (- 210.14μs / 26.4%)
Outliers: Lo: 0; Hi: 52; Sum: 52
Outlier variance: 0.435979 (moderate)
> Bootstrapped
Mean: 538.09μs (- 89.83μs / 14.3%)
Std deviation: 7827.00ns
> Relative
Difference at 95.0 confidence
-89.83μs ± 1076.00ns
-14.31% ± 0.17%
(Student's t, pooled s = 1.22739e+4
===> Testing bench_inline_type:bench_non_inline_function_head_match()
===> Stats for wall_time
Min: 742.92μs (- 100.83μs / 11.9%)
25 percentile: 755.54μs (- 95.96μs / 11.3%)
Median: 759.15μs (- 94.43μs / 11.1%)
75 percentile: 763.46μs (- 92.79μs / 10.8%)
Max: 867.23μs (- 157.60μs / 15.4%)
Outliers: Lo: 1; Hi: 67; Sum: 68
Outlier variance: 0.442993 (moderate)
> Bootstrapped
Mean: 761.35μs (- 94.94μs / 11.1%)
Std deviation: 11.25μs
> Relative
Difference at 95.0 confidence
-94.94μs ± 1074.00ns
-11.09% ± 0.13%
(Student's t, pooled s = 1.22503e+4
===> Testing bench_inline_type:bench_inline_function_head_match()
===> Stats for wall_time
Min: 502.11μs (- 111.71μs / 18.2%)
25 percentile: 508.84μs (- 112.41μs / 18.1%)
Median: 512.37μs (- 111.51μs / 17.9%)
75 percentile: 515.63μs (- 111.49μs / 17.8%)
Max: 569.84μs (- 160.03μs / 21.9%)
Outliers: Lo: 0; Hi: 70; Sum: 70
Outlier variance: 0.504817 (severe)
> Bootstrapped
Mean: 513.56μs (- 113.31μs / 18.1%)
Std deviation: 8567.00ns
> Relative
Difference at 95.0 confidence
-113.31μs ± 957.00ns
-18.08% ± 0.15%
(Student's t, pooled s = 1.09128e+4
===> Outlier variance is too high! Benchmark result might be non-representative. Try to repeat the benchmarks in more calm environment (no heavy background OS tasks) or run benchmark for a longer time
===> Testing bench_inline_type:bench_non_inline_best_case()
===> Stats for wall_time
Min: 792.25μs (- 101.57μs / 11.4%)
25 percentile: 806.00μs (- 96.73μs / 10.7%)
Median: 809.00μs (- 96.73μs / 10.7%)
75 percentile: 812.67μs (- 96.42μs / 10.6%)
Max: 996.58μs (- 93.78μs / 8.6%)
Outliers: Lo: 9; Hi: 85; Sum: 94
Outlier variance: 0.554059 (severe)
> Bootstrapped
Mean: 812.00μs (- 96.55μs / 10.6%)
Std deviation: 14.94μs
> Relative
Difference at 95.0 confidence
-96.55μs ± 1330.00ns
-10.63% ± 0.15%
(Student's t, pooled s = 1.51721e+4
===> Outlier variance is too high! Benchmark result might be non-representative. Try to repeat the benchmarks in more calm environment (no heavy background OS tasks) or run benchmark for a longer time
===> Testing bench_inline_type:bench_inline_best_case()
===> Stats for wall_time
Min: 547.00μs (- 114.53μs / 17.3%)
25 percentile: 554.94μs (- 113.32μs / 17.0%)
Median: 558.89μs (- 111.51μs / 16.6%)
75 percentile: 562.44μs (- 110.82μs / 16.5%)
Max: 621.83μs (- 148.57μs / 19.3%)
Outliers: Lo: 0; Hi: 70; Sum: 70
Outlier variance: 0.487813 (moderate)
> Bootstrapped
Mean: 560.15μs (- 112.63μs / 16.7%)
Std deviation: 9038.00ns
> Relative
Difference at 95.0 confidence
-112.63μs ± 895.00ns
-16.74% ± 0.13%
(Student's t, pooled s = 1.02065e+4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment