Skip to content

Commit 672fd37

Browse files
authored
readme: add example of improved run time performance (#6)
1 parent 2d6ac02 commit 672fd37

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,37 @@ julia> using FixFunctionArgument
3434
julia> sizeof(Fix1(convert, Float32))
3535
0
3636
```
37+
38+
Here is a more concrete, albeit artificial, example of a performance difference between `Fix` and `Base.Fix`.
39+
40+
```julia
41+
# calls `f` eight times
42+
function iterated_function_8(f::Func, x) where {Func}
43+
f = f f
44+
f = f f
45+
f = f f
46+
f(x)
47+
end
48+
49+
# like `identity`, but should not inline
50+
@noinline function identity_noinline(x)
51+
x
52+
end
53+
54+
# only does calling, for benchmarking call/stack overhead
55+
function identity_noinline_iterated(x)
56+
iterated_function_8(identity_noinline, x)
57+
end
58+
59+
using BenchmarkTools, FixFunctionArgument
60+
61+
x = Fix1(convert, Float32)
62+
y = Base.Fix1(convert, Float32)
63+
64+
@btime identity_noinline_iterated($x) # 1.292 ns (0 allocations: 0 bytes)
65+
@btime identity_noinline_iterated($y) # 16.222 ns (1 allocation: 16 bytes)
66+
67+
@code_typed identity_noinline_iterated(x) # shows that the entire call gets constant folded away
68+
```
69+
70+
Interpretation: for `Fix`, unlike for `Base.Fix`, the `identity_noinline_iterated` call has zero cost, because the entire call gets constant folded away.

0 commit comments

Comments
 (0)