I find that SpecialFunctions.jl’s besselk() is ~10X slower than MATLAB’s.
In MATLAB,
>> N = 10^6; v = rand(N,1);
>> tic; for x = v; besselk(1, x); end; toc
Elapsed time is 0.017116 seconds.
>> tic; for x = v; besselk(1, x); end; toc
Elapsed time is 0.017542 seconds.
With SpecialFunctions.jl,
julia> VERSION
v"1.10.2"
(@v1.10) pkg> st SpecialFunctions
Status `~/.julia/environments/v1.10/Project.toml`
[276daf66] SpecialFunctions v2.3.1
julia> using SpecialFunctions
julia> N = 10^6; v = rand(N);
julia> @time for x = v; SpecialFunctions.besselk(1, x); end;
0.158126 seconds (5.00 M allocations: 91.545 MiB, 1.42% gc time)
julia> @time for x = v; SpecialFunctions.besselk(1, x); end;
0.173226 seconds (5.00 M allocations: 91.545 MiB, 0.56% gc time)
BesselK.jl is better than SpecialFunctions.jl, but it still does not beat MATALB:
(@v1.10) pkg> st BesselK
Status `~/.julia/environments/v1.10/Project.toml`
[432ab697] BesselK v0.5.6
julia> using BesselK
julia> @time for x = v; BesselK.besselk(1, x); end;
0.066285 seconds (4.00 M allocations: 76.286 MiB, 1.60% gc time)
julia> @time for x = v; BesselK.besselk(1, x); end;
0.065575 seconds (4.00 M allocations: 76.286 MiB, 1.62% gc time)
Is there a way to make besselk() in Julia as fast as or faster than in MATLAB?
Another issue I found related to this is that besselk() is slower than besselj() in Julia, whereas it is similar in MATLAB.
In MATLAB,
>> tic; for x = v; besselj(1, x); end; toc
Elapsed time is 0.017452 seconds.
>> tic; for x = v; besselj(1, x); end; toc
Elapsed time is 0.018163 seconds.
so besselj() at a similar speed as besselk().
On the other hand, in Julia,
julia> @time for x = v; SpecialFunctions.besselj(1, x); end;
0.042908 seconds (4.00 M allocations: 76.286 MiB, 2.06% gc time)
julia> @time for x = v; SpecialFunctions.besselj(1, x); end;
0.044744 seconds (4.00 M allocations: 76.286 MiB, 1.15% gc time)
so bessej() is a lot faster than besselk(). (Despite this speedup compared to besselk(), SpecialFunctions.jl’s besselj() is still slower than MATLAB’s.)