Julia: A regression of the code that computes the sum of Leibniz series

Created on 7 Aug 2020  Â·  3Comments  Â·  Source: JuliaLang/julia

I come from https://discourse.julialang.org/t/more-upgrade-more-slow-down/44458/4.

I ran the following code from v1.0.5 to v1.5.0:

using BenchmarkTools

function powersign(i)
    if i % 2 == 0
        1
    else
        -1
    end
end

function leibniz(n)
    s = 0
    for i = 0:n
        s += powersign(i) / (2i + 1)
    end
    4s
end

n = 10^5
result = @benchmark leibniz($n)
show(stdout, MIME"text/plain"(), result)

and the results are

Intel Core i5-5250U / Ubuntu 18.04.5 | v1.0.5 | v1.1.1 | v1.2.0 | v1.3.1 | v1.4.2 | v1.5.0
-- | -- | -- | -- | -- | -- | --
minimum | 178.450 | 185.680 | 173.397 | 173.299 | 199.317 | 204.126
median | 179.032 | 186.778 | 179.037 | 179.031 | 199.456 | 204.186
mean | 182.881 | 189.929 | 183.635 | 183.448 | 203.279 | 208.294
maximum | 354.239 | 404.800 | 370.055 | 368.178 | 391.177 | 401.432
ratio of median to v1.0.5 | 1.000 | 1.043 | 1.000 | 1.000 | 1.114 | 1.141
ratio of mean to v1.0.5 | 1.000 | 1.039 | 1.004 | 1.003 | 1.112 | 1.139

Intel Core i7-7660U / macOS 10.15.6 | v1.0.5 | v1.1.1 | v1.2.0 | v1.3.1 | v.1.4.2 | v1.5.0
-- | -- | -- | -- | -- | -- | --
minimum | 117.882 | 126.724 | 125.325 | 125.344 | 150.376 | 136.314
median | 125.338 | 127.701 | 125.335 | 125.356 | 150.405 | 137.267
mean | 127.036 | 130.031 | 127.712 | 127.969 | 153.818 | 139.832
maximum | 903.391 | 343.934 | 402.069 | 436.871 | 451.494 | 349.415
ratio of median to v1.0.5 | 1.000 | 1.019 | 1.000 | 1.000 | 1.200 | 1.095
ratio of mean to v1.0.5 | 1.000 | 1.024 | 1.005 | 1.007 | 1.211 | 1.101

where time units are μs. v1.5.0 is slower than v1.0.5.

performance regression

Most helpful comment

And notice that the simd-version only became better since 1.0.5:

julia> function leibniz(n)
           s = 0.0
           @simd for i = 0:2:n
               s += 1 / (2i + 1)
           end
           @simd for i = 1:2:n
               s -= 1 / (2i + 1)
           end
           4s
       end

1.0.5:

julia> @btime leibniz(100000)
  50.324 μs (0 allocations: 0 bytes)

1.5.0:

julia> @btime leibniz(100000)
  46.797 μs (0 allocations: 0 bytes)

All 3 comments

Note that if you write the code in a type stable manner (changing s = 0 to s = 0.0) the timing is pretty much the same between 1.0 and 1.5:

1.0.5:

julia> @btime leibniz($n)
  93.264 μs (0 allocations: 0 bytes)

1.5.0:

julia> @btime leibniz($n)
  95.500 μs (0 allocations: 0 bytes)

And notice that the simd-version only became better since 1.0.5:

julia> function leibniz(n)
           s = 0.0
           @simd for i = 0:2:n
               s += 1 / (2i + 1)
           end
           @simd for i = 1:2:n
               s -= 1 / (2i + 1)
           end
           4s
       end

1.0.5:

julia> @btime leibniz(100000)
  50.324 μs (0 allocations: 0 bytes)

1.5.0:

julia> @btime leibniz(100000)
  46.797 μs (0 allocations: 0 bytes)

Looks like this is not a bug then

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yurivish picture yurivish  Â·  3Comments

wilburtownsend picture wilburtownsend  Â·  3Comments

StefanKarpinski picture StefanKarpinski  Â·  3Comments

m-j-w picture m-j-w  Â·  3Comments

tkoolen picture tkoolen  Â·  3Comments