Home > database >  why is the runtime 0 when using map in python
why is the runtime 0 when using map in python

Time:12-04

when I use the map function, the runtime is 0. but when I do the same operation using FOR, it displays the time taken for the script to run.

def forthpower(n):

    return n*n*n*n

t2 = time.time()
xx = map(forthpower, range(10000000))
print("The process took {:1.10f}".format(time.time()-t2))

output: The process took 0.0000000000

def forthpower(n):

    return n*n*n*n

t2 = time.time()
xx = []
for x in range(10000000):
    xx.append(forthpower(x))

print("The process took {:1.10f}".format(time.time()-t2))

output: The process took 10.5718030930

CodePudding user response:

As Klaus D. said, map() is an iterator, it will process data only when requested.

If you add:

print(list(xx))

you'll start seing a difference.

  • Related