# Numba 加速蒙特卡罗模拟

## 在 Python 中使用 numba 进行蒙特卡罗模拟

``````import numpy as np
from matplotlib import pyplot as plt
import random
import time
from numba import njit

def random_walker(n: int, length: int) -> np.ndarray:

arr = np.zeros((n, length), dtype = float)

for i in range(n):

idx = 100

for j in range(length):

idx += idx*random.gauss(0.00015, 0.02)

arr[i, j] = idx

return arr

@njit
def nb_random_walker(n: int, length: int) -> np.ndarray:

arr = np.zeros((n, length), dtype = float)

for i in range(n):

idx = 100

for j in range(length):

idx += idx*random.gauss(0.00015, 0.02)

arr[i, j] = idx

return arr``````

``````# --- monte carlo run without numba ------------------
starttime = time.time()
arr = random_walker(10000, 365)
endtime = time.time()

print("monte carlo random walk without NUMBA: ")
print(endtime-starttime)

# --- monte carlo run with numba --------------------
starttime = time.time()
arr = nb_random_walker(10000, 365)
endtime = time.time()

print("monte carlo random walk with NUMBA: ")
print(endtime-starttime)``````

``````monte carlo random walk without NUMBA:
1.9181511402130127
monte carlo random walk with NUMBA:
0.6535243988037109``````