Reputation: 369
Profiling this code shows the bulk of the time is spent on the log operation. Is there another way to write this in Python 3 for more efficiency? Replacing the loop with a list comprehension was actually less efficient and so was map because of lambdas.
def log_total(data):
total = 0.0
log = log(data)
for i in range(10000):
total += log/(i+1)
return total
Thanks!
Upvotes: 3
Views: 286
Reputation:
Can write with lambda
in on line, like this:
total = lambda data: log(data) * sum(1.0 / i for i in xrange(1, 10001))
I used Python 2.7.3
.
Upvotes: 0