I want to make my code run faster for more iterations and runs. Right now my code is too slow, but I don't know what to change to speed it up. I began by coding a kinetic Monte Carlo simulation, then editing it to become a Brownian motion simulation. My current code can't handle 10,000 runs with 10,000 iteration each, which is needed.
import numpy as np
import matplotlib.pyplot as plt
import time
%matplotlib inline
runs = int(input("Enter number of runs: "))
N = int(input("Enter number of iterations per simulation: "))
y = 0
R = 10*1 # R is the rate value
t0 = time.time()
for y in range(runs): # Run the simulation 'runs' times
T = np.array([0])
dt = 0
x = 0.5 # sets values
X = np.array([x])
t = 0
i = 0
while t < N: # N is the number of iterations per run
i = i + 1 # i is number of iterations so far
z = np.random.uniform(-1, 1, 1) # sets z to be a random number between -1 to 1 size 1
if z > (1/3): # if conditions for z for alpha and gamma, beta
x = x + 1 # z[]=alpha state then + 1
elif z < (-1/3):
x = x-1 # z[]=gamma state then - 1
elif z < (1/3) and z > (-1/3):
x = x # z=beta state then + 0
X = np.append(X, x) # adds new X value to original X array
X[i] += X[i-1] * 0.01 * np.random.normal(0, 1, 1) * np.sqrt(dt) # for Brownian motion with sigma as 0.01
Z = np.random.uniform(0, 1) # sets Z to be a random number between 0 and 1
dt = 1/R * np.log(1/Z) # formula for dt; R is the rate value
t = t + dt # ITERATED TIME
T = np.append(T, t)
plt.plot(T, X, lw='0.5', alpha=0.5)
t1 = time.time()
print("final %.10f seconds " % (t1-t0))