Last active
December 28, 2020 23:42
-
-
Save Sixzero/149e391b41879ac10d0952d6e0e97fab to your computer and use it in GitHub Desktop.
opaque_closure_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%% | |
using BenchmarkTools | |
BenchmarkTools.DEFAULT_PARAMETERS.seconds = 2.50 | |
using Base: @opaque | |
add_opaq = @opaque (a::Vector, b::Vector) -> a .+ b | |
@noinline add(a::Vector, b::Vector) = a .+ b | |
@noinline add!(c::Vector, a::Vector, b::Vector) = c .= a .+ b | |
@inline add_inline(a::Vector, b::Vector) = a .+ b | |
#%% | |
test() = begin | |
a = randn(1024) | |
b = randn(1024) | |
c = randn(1024) | |
@btime $c .= $add($a, $b) | |
@btime $c .= $add!($c, $a, $b) | |
@btime $c .= $add_inline($a, $b) | |
@btime $c .= $add_opaq($a, $b) | |
end | |
test() | |
; | |
# 816.818 ns (1 allocation: 8.12 KiB) | |
# 126.728 ns (0 allocations: 0 bytes) | |
# 857.111 ns (1 allocation: 8.12 KiB) | |
# 4.096 μs (11 allocations: 8.48 KiB) | |
#%% | |
using Base: @opaque | |
test2 = @opaque () -> begin | |
a = randn(1024) | |
b = randn(1024) | |
c = randn(1024) | |
@time c .= add(a, b) | |
@time c .= add!(c, a, b) | |
@time c .= add_inline(a, b) | |
@time c .= add_opaq(a, b) | |
end | |
test2() | |
; | |
# 0.000037 seconds (9 allocations: 8.344 KiB) | |
# 0.000037 seconds (8 allocations: 224 bytes) | |
# 0.000043 seconds (9 allocations: 8.344 KiB) | |
# 0.000074 seconds (16 allocations: 8.672 KiB) |
You are right, it is worth a try, so:
@btime $c .= $add_opaq($a, $b)
# 4.064 μs (11 allocations: 8.53 KiB)
@btime $c .= add_opaq($a, $b)
# 4.096 μs (10 allocations: 8.48 KiB)
Interestingly it increased allocations by 1.
One of my friend told me, that in C++ we have "move semantics" and "return value optimization" developed to prevent these allocations. What we need to know why julia can't control the LLVM to apply the optimizations in such situations.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maybe you need
@btime $c .= $add_opaq($a, $b)
sinceadd_opaq
is a non-constant global?