Simple brownian motion plot is insanely slow. How can I speed it up?

using LinearAlgebra, Plots, Distributions, Random; 
Δt=0.01; T_sim=500; N_sim = 100; 
time_sim = 0.0:Δt:T_sim
ε_sim = randn(MersenneTwister(123), N_sim, length(time_sim)); 
Z_sim =cumsum(ε_sim, dims=2)

plt_shock = plot(legend=:topright)
plot!(time_sim, Z_sim', lab="")

image
Plotting this simple simulation is insanely slow.
How can I speed it up?

What do you mean by insanely slow? The time to generate the first plot in a new session, or the time to generate any plot (after initial compilation)?

If the issue is the former, look into custom sysimages (or maybe try nightly now that native code caching has landed).

If it’s the latter, notice that you are plotting 5 million points if my math is right, which is bound to take some time. You can look at GPU accelerated plotting via WLMakie, or simply plot fewer points (there are only around 2m pixels on a 1,920x1,080 screen so you’re unlikely to distinguish 5m points anyway!)

1 Like

I didn’t mean TTFP, I meant the latter.
There has gotta be a way for visualizations in Julia to realize I don’t need such a fine grid & speed it up…

You can try a different backend for Plots.jl to see which is faster, off the top of my head, I’m not sure which backend will be best for this task.

From my experience one of the Makie packages (i.e GLMakie or WGLMakie as @nilshg suggested) will probably be best if you want faster plots with millions of points, but this has a higher TTFP than Plots.jl.

Sysimages will help with the latency as well.

Do you really need to plot all those points? With that many ensembles, it is usually better to plot quantiles/means/etc.

I find using the SciML ecosystem is the easiest way to get things done and the plotting recipes with ensembles work great. And if you setup the brownian motion as a drift free SDE there are better algorithms for simulating them.

See Stochastic Differential Equations · DifferentialEquations.jl

And 42. Modeling Shocks in COVID 19 with Stochastic Differential Equations — Quantitative Economics with Julia

It plots what you tell it to plot. If you want to plot less detail, it’s up to you to pass in a simplified data series.

But for good measure, is Plots at the most recent version? There were some serious slowdowns as a function of series number and series size as recently as a year ago.