I found it was slow when I used QuadGK.jl to do integral calculation. Then I used MATLAB to make a comparison and found that Julia integral was slower than MATLAB. Here are my comparisons:
julia> using QuadGK
julia> f(x) = (cos(x)^5 - cos(x)^3 + cos(x))*sin(x)^5
f (generic function with 1 method)
julia> @time quadgk(f, -1, 1, rtol=1e-14)
0.499007 seconds (609.20 k allocations: 34.800 MiB, 1.39% gc time, 99.96% compilation time)
(6.713414395092624e-19, 0.0)
julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, skylake)
The above tests were performed on the same computer.
Julia integrals are slower on Linux:
julia> using QuadGK
julia> f(x) = (cos(x)^5 - cos(x)^3 + cos(x))*sin(x)^5
f (generic function with 1 method)
julia> @time quadgk(f, -1, 1, rtol=1e-14)
2.708747 seconds (7.27 M allocations: 175.746 MiB, 1.04% gc time, 20.33% compilation time)
(-5.403687448405256e-18, 5.524260415282572e-29)
julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i9-7980XE CPU @ 2.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, skylake-avx512)
Of course what I’m showing here is just integrating against a particular function, but when I do a lot of integrals Julia is obviously much slower than MATLAB. Is it my improper use of the quadgk function or is this package not perfect? Do you have any good suggestions?
You are using absolute error tolerance in Matlab (AbsTol) and relative tolerance in QuadGK (rtol). Since the integral goes to zero, QuadGK won’t give up until the error is actually 0.0.
Use atol instead. Or, perhaps, bothrtol and atol, to make sure you get convergence both in cases with zero result and with non-zero results.
After compilation? You means run a second time? The second time, it does come out faster. But if you integrate the function g(x) =sin(x) * cos(x) , you’ll see that no matter how many runs you do, you’re not going to get a significant increase in speed