I know that C++ should be much faster than Python 3 because it is a compiled language as opposed to an interpreted language.
I wrote 2 two programs that use the Monte
Not meant as an answer to your question why python is faster, just to show that python can get event faster and neater for this problem.
To possibilities to speed things up in python:
Use numpy vectorization:
import numpy as np
def pi(N):
x, y = np.random.uniform(-1, 1, size=(2, N))
in_circle = np.count_nonzero(x**2 + y**2 <= 1)
return 4 * in_circle / N
And / or numba just in time compilation:
from numba import jit
import random
@jit
def pi(N):
in_circle = 0
for i in range(N):
x = 2 * random.random() - 1
y = 2 * random.random() - 1
if x**2 + y**2 <= 1:
in_circle += 1
return 4 * in_circle / N