# Is Julia integral calculation not as fast as MATLAB?

**URL:** <https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824>\
**Category:** Performance\
**Tags:** quadgk, integral\
**Created:** [April 22, 2022, 9:13am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824 "2022-04-22T09:13:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [April 22, 2022, 9:13am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/1 "2022-04-22T09:13:55Z")

</div>

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
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)

```

Results in MATLAB:

```julia
f = @(x) ((cos(x).^5 - cos(x).^3 + cos(x)) .* sin(x).^5);

tic
    quadgk(f, -1, 1,'AbsTol',1e-14);
toc

output: 0.145103s

```

The above tests were performed on the same computer.

Julia integrals are slower on Linux:

```julia
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?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 9:25am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/2 "2022-04-22T09:25:54Z")

</div>

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, _both_ `rtol` and `atol`, to make sure you get convergence both in cases with zero result and with non-zero results.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 22, 2022, 9:27am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/3 "2022-04-22T09:27:14Z")

</div>

After compilation Julia gets faster:

```julia
using QuadGK
using BenchmarkTools

f(x) = (cos(x)^5 - cos(x)^3 + cos(x))*sin(x)^5

@btime quadgk(f, -1, 1, rtol=1e-14)

```

yielding

```julia
  140.000 μs (958 allocations: 25.11 KiB)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 9:30am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/4 "2022-04-22T09:30:34Z")

</div>

```julia
julia> @btime quadgk(f, -1, 1; rtol=1e-14, atol=1e-14)
  2.822 μs (7 allocations: 192 bytes)
(-2.304928352082297e-18, 1.5934560307793533e-18)

```

Fast enough? (Note: microseconds.)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 9:33am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/5 "2022-04-22T09:33:43Z")

</div>

> [@goerch](#):
>
> After compilation Julia gets faster:

Hmm. With `rtol` I’m actually seeing this:

```julia
1.7.2> @btime quadgk(f, -1, 1, rtol=1e-14)
  2.269 s (6666686 allocations: 141.08 MiB)
(-5.403687448405256e-18, 5.524260415282572e-29)

```

As you can see, the convergence criterium is crazy. This is why we should use `atol`. And that is clearly insanely fast.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 22, 2022, 9:37am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/6 "2022-04-22T09:37:26Z")

</div>

I’m wondering

```julia
using QuadGK
using BenchmarkTools

f(x) = (cos(x)^5 - cos(x)^3 + cos(x))*sin(x)^5

@btime quadgk(f, -1, 1, atol=1e-14)
@btime quadgk(f, -1, 1, rtol=1e-14)
versioninfo()

```

yielding

```julia
  1.420 μs (6 allocations: 160 bytes)
  158.400 μs (954 allocations: 25.05 KiB)
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-10710U CPU @ 1.10GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 4

```

Will check with newer versions…

Edit: looks even slightly better on nightly for me.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 9:40am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/7 "2022-04-22T09:40:15Z")

</div>

I’m also on 1.7.2. Windows.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 22, 2022, 9:42am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/8 "2022-04-22T09:42:10Z")

</div>

```julia
julia> Pkg.status("QuadGK")
Status `...\Project.toml`
  [1fd47b50] QuadGK v2.4.2

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 9:44am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/9 "2022-04-22T09:44:02Z")

</div>

```julia
1.7.2> 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-9750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

(@v1.7) pkg> st QuadGK
  [1fd47b50] QuadGK v2.4.2

```

Strange. Especially the `rtol`, but also `atol` is twice as slow on my computer, and that seems odd.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 22, 2022, 9:46am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/10 "2022-04-22T09:46:33Z")

</div>

> [@DNF](#):
>
> Especially the `rtol` , but also `atol` is twice as slow on my computer, and that seems odd.

I was wondering about

> [@DNF](#):
>
> Hmm. With `rtol` I’m actually seeing this:
> 
> ```julia
> 1.7.2> @btime quadgk(f, -1, 1, rtol=1e-14)
> 2.269 s (6666686 allocations: 141.08 MiB)
> (-5.403687448405256e-18, 5.524260415282572e-29)
> 
> ```

which looks to be more than 2x slower?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 9:48am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/11 "2022-04-22T09:48:14Z")

</div>

Yes, _especially_ `rtol`, but I’m also perplexed about the `atol` difference.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 9:50am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/12 "2022-04-22T09:50:27Z")

</div>

> [@F-YF](#):
>
> ```julia
> f = @(x) ((cos(x).^5 - cos(x).^3 + cos(x)) .* sin(x).^5);
> 
> tic
> quadgk(f, -1, 1,'AbsTol',1e-14);
> toc
> 
> output: 0.145103s
> 
> ```

BTW, the Matlab timing is also off. Here’s mine:

```julia
>> timeit(@()quadgk(f, -1, 1,'AbsTol',1e-14))
ans =
   3.5025e-04

```

That’s 350us.

And the new `integral` function is faster yet:

```julia
>> timeit(@()integral(f, -1, 1,'AbsTol',1e-14))
ans =
   2.0296e-04

```

But still 100x slower than QuadGK.jl 😉

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 22, 2022, 10:01am UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/13 "2022-04-22T10:01:59Z")

</div>

> [@DNF](#):
>
> Yes, _especially_ `rtol` , but I’m also perplexed about the `atol` difference.

```julia
JULIA_NUM_THREADS = 4

```

?

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [April 22, 2022, 12:26pm UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/14 "2022-04-22T12:26:16Z")

</div>

> [@DNF](#):
>
> Use `atol` instead. Or, perhaps, _both_ `rtol` and `atol` , to make sure you get convergence both in cases with zero result and with non-zero results.

It’s actually faster.

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [April 22, 2022, 12:30pm UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/15 "2022-04-22T12:30:46Z")

</div>

> [@goerch](#):
>
> After compilation Julia gets faster:
> 
> ```julia
> using QuadGK
> using BenchmarkTools
> 
> f(x) = (cos(x)^5 - cos(x)^3 + cos(x))*sin(x)^5
> 
> @btime quadgk(f, -1, 1, rtol=1e-14)
> 
> ```
> 
> yielding
> 
> ```julia
> 140.000 μs (958 allocations: 25.11 KiB)
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 1:03pm UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/16 "2022-04-22T13:03:26Z")

</div>

> [@F-YF](#):
>
> It’s actually faster.

What is faster?

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [April 22, 2022, 1:12pm UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/17 "2022-04-22T13:12:46Z")

</div>

Use `atol` instead，or both `rtol` and `atol`，which will faster than use `rtol` only

> [@DNF](#):
>
> What is faster?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 22, 2022, 1:16pm UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/18 "2022-04-22T13:16:47Z")

</div>

> [@F-YF](#):
>
> 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

Second run is faster:

```julia
julia> g(x) = sin(x) * cos(x)
g (generic function with 1 method)

julia> @time quadgk(g, -1, 1; atol=1e-14)
  0.427068 seconds (462.03 k allocations: 26.545 MiB, 99.99% compilation time)
(-1.2855363932200686e-17, 1.51605683459074e-17)

julia> @time quadgk(g, -1, 1; atol=1e-14)
  0.000005 seconds (6 allocations: 160 bytes)
(-1.2855363932200686e-17, 1.51605683459074e-17)

```

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [April 22, 2022, 1:23pm UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/19 "2022-04-22T13:23:46Z")

</div>

> [@DNF](#):
>
> > [@F-YF](#):
> >
> > 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
> 
> Second run is faster:
> 
> ```julia
> julia> g(x) = sin(x) * cos(x)
> g (generic function with 1 method)
> 
> julia> @time quadgk(g, -1, 1; atol=1e-14)
> 0.427068 seconds (462.03 k allocations: 26.545 MiB, 99.99% compilation time)
> (-1.2855363932200686e-17, 1.51605683459074e-17)
> 
> julia> @time quadgk(g, -1, 1; atol=1e-14)
> 0.000005 seconds (6 allocations: 160 bytes)
> (-1.2855363932200686e-17, 1.51605683459074e-17)
> 
> ```

It does faster with ‘atol’,

> [@F-YF](#):
>
> > [@goerch](#):
> >
> > After compilation Julia gets faster:
> > 
> > > using QuadGK  
> > > using BenchmarkTools  
> > > f(x) = (cos(x)^5 - cos(x)^3 + cos(x))\*sin(x)^5  
> > > @btime quadgk(f, -1, 1, rtol=1e-14)
> > 
> > > yielding  
> > > 140.000 μs (958 allocations: 25.11 KiB)
> 
> 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

It’s using ‘rtol’ here

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 22, 2022, 1:49pm UTC](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824/20 "2022-04-22T13:49:12Z")

</div>

> [@goerch](#):
>
> `JULIA_NUM_THREADS = 4`

(QuadGK doesn’t use multiple threads.)

[Next page](https://discourse.julialang.org/t/is-julia-integral-calculation-not-as-fast-as-matlab/79824.md?page=2)
