# Why is this simple code slow (how to speed it up)

**URL:** https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854
**Category:** Performance
**Created:** [February 6, 2018, 3:28am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854 "2018-02-06T03:28:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![cpraveen](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@cpraveen](https://discourse.julialang.org/u/cpraveen)
#### Post date: [February 6, 2018, 3:28am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854/1 "2018-02-06T03:28:37Z")

</div>

This small code takes about 24 seconds every time on my macbook pro. Is there a way to make it faster ? I run it on commend line as

```julia
julia test.jl

```

```julia
using Plots
x = linspace(0.0,2*pi,100);
y = sin.(x);
plot(x,y)
savefig("test.pdf")

```

---

<div class="post-metadata">

### Author: ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)
#### Post date: [February 6, 2018, 3:42am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854/2 "2018-02-06T03:42:45Z")

</div>

I guess… no…?

You can try switch backend to GR.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [February 6, 2018, 3:47am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854/3 "2018-02-06T03:47:27Z")

</div>

The first time you run any function in Julia, the function is compiled. Plots functions are particularly heavy when run the first time and since you are starting Julia, calling the functions once and closing Julia again, you will be paying this price every time. So just plot from Jupyter or something so you only pay the compilation price once and then enjoy Julia’s true speed!

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [February 6, 2018, 4:01am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854/4 "2018-02-06T04:01:27Z")

</div>

You’ve hit on a significant pain point in Julia development right now. Running a plotting command from the command line is pretty much the worst possible case because you’re paying the entire overhead of compiling the (relatively large) plotting libraries for just one plot. As others have suggested, you’ll find that the _second_ plot command is super quick, so keeping your `julia` session running longer will definitely help. Jupyter (with [IJulia.jl](https://github.com/JuliaLang/IJulia.jl)) is one excellent way to do this, and it’s what I personally use.

This is, however, still painful, and it’s something that will likely be a high priority once the API changes for the upcoming v1.0 are completed. There’s been some great work on fully pre-compiling packages, such as [https://github.com/SimonDanisch/PackageCompiler.jl](https://github.com/SimonDanisch/PackageCompiler.jl) and I suspect we’ll see a lot more soon.

If you want to know all the details about _why_ that code is slow, Jameson’s JuliaCon talk is very informative: [JuliaCon 2017 | AoT or JIT: How Does Julia Work? | Jameson Nash - YouTube](https://www.youtube.com/watch?v=7KGZ_9D_DbI)

---

<div class="post-metadata">

### Author: ![cpraveen](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@cpraveen](https://discourse.julialang.org/u/cpraveen)
#### Post date: [February 6, 2018, 4:04am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854/5 "2018-02-06T04:04:23Z")

</div>

GR is also as slow. I dont like to use jupyter but I can run it in REPL which is fast in subsequent runs. Thanks.

---

<div class="post-metadata">

### Author: ![cpraveen](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@cpraveen](https://discourse.julialang.org/u/cpraveen)
#### Post date: [February 6, 2018, 4:11am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854/6 "2018-02-06T04:11:28Z")

</div>

In REPL I run the code as

```julia
julia> include("./test.jl")

```

Is this the best way to run a julia file ?

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [February 6, 2018, 4:41am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854/7 "2018-02-06T04:41:30Z")

</div>

You can put it in a module in your path.

```julia
__precompile__ ()
module foo

using Plots

export bar, foobar

function bar(n = 100, l = 0.0, u = 2pi)
    x = linspace(l, u, n)
    x, sin.(x)
end

function foobar(filename="test.pdf", n=100, l = 0.0, u = 2pi)
    x, y = bar(l, u, n)
    plot(x, y)
    savefig(filename)
end

end #module

```

Now

```julia
using Revise, foo
foobar()
# edit and save the file foo.jl perhaps using @edit foobar
foobar() #Revise updates to new version you saved automatically

```

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [February 6, 2018, 4:44am UTC](https://discourse.julialang.org/t/why-is-this-simple-code-slow-how-to-speed-it-up/8854/8 "2018-02-06T04:44:42Z")

</div>

That’s what I do all the time - leaving the REPL up and keep using it for a long time.

Just make sure that the code does not reference any global variable and if so you may be surprised with different behavior when the global variable changes in the environment.
