Why is this Python script with Matplotlib so slow?

  • A+

I'm trying so simulate coin tosses and profits and plot the graph in matplotlib:

from random import choice import matplotlib.pyplot as plt import time  start_time = time.time() num_of_graphs = 2000 tries = 2000 coins = [150, -100] last_loss = 0   for a in range(num_of_graphs):     profit = 0     line = []     for i in range(tries):         profit = profit + choice(coins)         if (profit < 0 and last_loss < i):             last_loss = i         line.append(profit)     plt.plot(line) plt.show()  print("--- %s seconds ---" % (time.time() - start_time)) print("No losses after " + str(last_loss) + " iterations") 

The end result is

--- 9.30498194695 seconds --- No losses after 310 iterations 

Why is it taking so long to run this script? If I change num_of_graphs to 10000, the scripts never finishes.

How would you optimize this?

Why is this Python script with Matplotlib so slow?


Your measure of execution time is too rough. The following allows you to measure the time needed for the simulation, separate from the time needed for plotting:

It is using numpy.

import matplotlib.pyplot as plt import numpy as np import time   def run_sims(num_sims, num_flips):     start = time.time()     sims = [np.random.choice(coins, num_flips).cumsum() for _ in range(num_sims)]     end = time.time()     print(f"sim time = {end-start}")     return sims   def plot_sims(sims):     start = time.time()     for line in sims:         plt.plot(line)     end = time.time()     print(f"plotting time = {end-start}")     plt.show()   if __name__ == '__main__':      start_time = time.time()     num_sims = 2000     num_flips = 2000     coins = np.array([150, -100])      plot_sims(run_sims(num_sims, num_flips)) 


sim time = 0.13962197303771973 plotting time = 6.621474981307983 

As you can see, the sim time is greatly reduced (it was on the order of 7 seconds on my 2011 laptop); The plotting time is matplotlib dependent.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: