Last active
November 20, 2018 21:07
-
-
Save nlw0/e3c94ffd816b5592736c0e822f0424be to your computer and use it in GitHub Desktop.
Exponential function benchmark in Julia
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
@inline @fastmath function myexpm1(x :: Float64) | |
ln2_hi = 6.93147180369123816490e-01 | |
ln2_lo = 1.90821492927058770002e-10 | |
one = 1.0 | |
Q1 = -3.33333333333331316428e-02 | |
Q2 = 1.58730158725481460165e-03 | |
Q3 = -7.93650757867487942473e-05 | |
Q4 = 4.00821782732936239552e-06 | |
Q5 = -2.01099218183624371326e-07 | |
hi = x - ln2_hi | |
lo = ln2_lo | |
k = 1 | |
x = hi - lo; | |
c = (hi - x) - lo; | |
hfx = 0.5 * x; | |
hxs = x * hfx; | |
R1 = one + hxs * Q1; | |
h2 = hxs * hxs; | |
R2 = Q2 + hxs * Q3; | |
h4 = h2 * h2; | |
R3 = Q4 + hxs * Q5; | |
r1 = R1 + h2 * R2 + h4 * R3; | |
t = 3.0 - r1 * hfx; | |
e = hxs * ((r1 - t) / (6.0 - x * t)); | |
e = (x * (e - c) - c); | |
e -= hxs; | |
one + 2.0 * (x - e); | |
end | |
@inline myexp(x) = myexpm1(x) + 1 | |
@inline calcerr(x) = abs(myexp(x) - exp(x)) | |
@inline function test_myexp_for(n) | |
err = 0.0 | |
for i in 0:n | |
err = max(err, calcerr(0.4+0.6*i/n)) | |
end | |
err | |
end | |
@inline function test_myexp_fold(n) | |
@inline errmax(acc, i) = max(acc, calcerr(0.4+0.6*i/n)) | |
foldl(errmax, 0:n, init=0) | |
end | |
for n in reverse([1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9]) | |
tt = @elapsed er=test_myexp_for(convert(Int, n)) | |
println("($n, $tt, $er)") | |
end | |
for n in reverse([1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9]) | |
tt = @elapsed er=test_myexp_fold(convert(Int, n)) | |
println("($n, $tt, $er)") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment