Hello i am trying to speed up this: x::Bool ^ y::Float64
, and even something come out
Is there any way to avoid branching of f2() in example?
How else to speed up?
using BenchmarkTools, Test
# simple
f1(x::Bool, y::Float64) = x ^ y;
# faster
function f2(x::Bool, y::Float64)
if x
return 1.0 # true ^ any_float64 -> 1.0
elseif isnan(y)
return NaN # false ^ NaN -> NaN
elseif y > 0
return 0.0
elseif y < 0
return Inf
else # y == 0
return 1.0
end
end;
# test data
n = 10000;
r = zeros(Float64, n); # for result
x = rand(Bool, n);
y = randn(Float64, n) .^ 111;
y[rand(1:n, 100)] .= NaN;
y[rand(1:n, 100)] .= -Inf;
y[rand(1:n, 100)] .= Inf;
y[rand(1:n, 100)] .= nextfloat(-Inf);
y[rand(1:n, 100)] .= prevfloat(Inf);
y[rand(1:n, 100)] .= 0.0;
y[rand(1:n, 100)] .= -0.0;
#test
@testset begin
for i = 1 : n
@test f1(x[i],y[i]) === f2(x[i],y[i])
end
end;
# benchmarks
f!(f,r,x,y,n) = for i = 1 : n
r[i] = f(x[i], y[i])
end;
@btime f!($f1,$r,$x,$y,$n) # -> 243.600 μs (0 allocations: 0 bytes)
@btime f!($f2,$r,$x,$y,$n) # -> 5.783 μs (0 allocations: 0 bytes)