代碼
import time
import random
hits=0
pi=0
DARTS=100000*100000
start=time.perf_counter()
for i in range(DARTS):x,y=random.random(),random.random()dist=pow(x ** 2+y**2,0.5)if dist <= 1.0:hits+=1
pi=4*(hits/DARTS)
print("圓周率的值是{:.10f}".format(pi))
print("程序運行時間為{}s".format(time.perf_counter()-start))
100萬個數據
100億個數據
三個半小時運行時間