4
I was playing with a method to approximate the PI number and I realized that the code runs very slowly in the Python 3.6 interpreter, and the process is only using 30% of the processor.
There would be a way to run this script in full swing?
from math import sqrt
from random import random
piavg = 0
samples = 10000
scale = 1000
progress = 0
debug_rate = 5
debug_clock = 0
def pis (scale):
pi = 0
points = []
for i in range(scale):
points.append([random(),random()])
for p in points:
if sqrt(p[0] ** 2 + p[1] ** 2) <= 1:
pi += 1
pi /= scale
pi *= 4
return pi
for i in range(samples):
cpi = pis(scale)
progress += 1/samples*100
if debug_rate > 0 and debug_clock == 0:
print(str(int(progress)) + "% encontrado: " + str(cpi))
debug_clock = debug_rate
piavg += cpi
debug_clock -= 1
piavg /= samples
print (piavg)
Very well Luiz, what a distraction not to have noticed that.
– Miguel
By combining our solutions we have managed to half, +- 4 secs, the execution time in relation to the question code, and your suggestion was the one that contributed the most
– Miguel
wow, I had forgotten this property sqrt(xx) < y == xx < y*y, well thought out!
– StackOverflowToxicityVictim