Are higher order functions and Lambdas using uptime and memory efficiency better or worse? For example, to multiply all the numbers in a list:
nums = [1,2,3,4,5] prod = 1 for n in nums: prod*=n
against
prod2 = reduce(lambda x,y:x*y , nums)
Does the HOF version have an advantage over the loop version other than smaller lines of code / uses a functional approach?
EDIT:
I cannot add this as an answer since I do not have the necessary reputation. I tied a loop and HOF approach to the profile using timeit, as suggested by @DSM
def test1(): s= """ nums = [a for a in range(1,1001)] prod = 1 for n in nums: prod*=n """ t = timeit.Timer(stmt=s) return t.repeat(repeat=10,number=100) def test2(): s=""" nums = [a for a in range(1,1001)] prod2 = reduce(lambda x,y:x*y , nums) """ t = timeit.Timer(stmt=s) return t.repeat(repeat=10,number=100)
And this is my result:
Loop: [0.08340786340144211, 0.07211491653462579, 0.07162720686361926, 0.06593182661083438, 0.06399049758613146, 0.06605228229559557, 0.06419744588664211, 0.0671893658461038, 0.06477527090075941, 0.06418023793167627] test1 average: 0.0644778902685 HOF: [0.0759414223099324, 0.07616920129277016, 0.07570730355421262, 0.07604965128984942, 0.07547092059389193, 0.07544737286604364, 0.075532959799953, 0.0755039779810629, 0.07567424616704144, 0.07542563650187661] test2 average: 0.0754917512762
On average, the loop approach is faster than using HOF.