# Plotting inside a loop is extremely slow

**URL:** https://discourse.julialang.org/t/plotting-inside-a-loop-is-extremely-slow/99948
**Category:** New to Julia
**Created:** [June 6, 2023, 3:04pm UTC](https://discourse.julialang.org/t/plotting-inside-a-loop-is-extremely-slow/99948 "2023-06-06T15:04:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Trandul](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@Trandul](https://discourse.julialang.org/u/Trandul)
#### Post date: [June 6, 2023, 3:04pm UTC](https://discourse.julialang.org/t/plotting-inside-a-loop-is-extremely-slow/99948/1 "2023-06-06T15:04:04Z")

</div>

Greetings everyone. I am a new and young programmer coming from matlab and I am trying to understand the basics of Julia. I have come across the following problem.  
I used a function to make a loop and I would like to make a simple plot of x and z in which z is updated in each loop. Whenever I run the code without plotting it runs smoothly and very fast. Whenever I try to plot the code becomes super slow and each iteration takes ages. What could be the problem?! Thank you very much

```julia
using Plots
function f()
 x = 1:1:10
 y = rand(10)
z = fill(0,10)
 d = 0
for i in 1:10000
    d = d + 1
     z = z .+ 2*d*y
    println(z)
   p = plot!(x,z)
    display(p)
    sleep(1000)
end
end
f()
```

---

<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: [June 6, 2023, 3:07pm UTC](https://discourse.julialang.org/t/plotting-inside-a-loop-is-extremely-slow/99948/2 "2023-06-06T15:07:50Z")

</div>

> [@Trandul](#):
>
> `sleep(1000)`

For each iteration you put it to sleep for 1000 seconds 😉

It should finish in 4 months or so😁

---

<div class="post-metadata">

### Author: ![Trandul](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@Trandul](https://discourse.julialang.org/u/Trandul)
#### Post date: [June 6, 2023, 3:11pm UTC](https://discourse.julialang.org/t/plotting-inside-a-loop-is-extremely-slow/99948/3 "2023-06-06T15:11:24Z")

</div>

Oops, I thought sleep is how many iterations I want it to skip so that the next plot can be shown. Thanks ahhahaha

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 6, 2023, 3:19pm UTC](https://discourse.julialang.org/t/plotting-inside-a-loop-is-extremely-slow/99948/4 "2023-06-06T15:19:27Z")

</div>

As a general tip, a quick doc search in the REPL is always helpful, just hit `?` in the REPL to enter help mode. In this case:

```julia
help?> sleep()
  sleep(seconds)

  Block the current task for a specified number of seconds. The minimum sleep time is 1 millisecond or input of 0.001.

```
