I'm interested in whether there is a way to further improve a "fast" version of a function used in a homework assignment I received recently (I've already submitted the completed work).
from math import log
def func_fast(mass, density):
return sum(map((log(mass * density)).__truediv__, range(1,10001)))
def func_slow(mass, density):
total = 0.0
for i in range(10000):
masslog = log(mass * density)
total += masslog/(i+1)
return total
mass = 2.5
density = 12.0
The fast version times in around 2-2.5ish seconds while the slow version nets 6-7 seconds.