Plotting inside a loop is extremely slow

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

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

For each iteration you put it to sleep for 1000 seconds :wink:

It should finish in 4 months or so😁

4 Likes

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

3 Likes

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:

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.
4 Likes