Respuesta :
This code generates two lists with 1 million elements each by randomly generating integers between -5 million and 5 million (inclusive).
# import modules for timing and generating random numbers
import time
import random
# set lower and upper bounds to generate random numbers
lower_bound=-5000000
upper_bound=5000000
# generate list 1 of size 1 million with random numbers between the given lower and upper bound
list_1=[]
for i in range(1000000):
x=random.randint(lower_bound,upper_bound)
list_1.append(x)
# generate list 2 with the squares of list 1
list_2=[x**2 for x in list_1]
# generate list 3 with tuples where first element is from list 1 and second element is from list 2 and every element is greater than 450000
start_time=time.time()
start_cpu=time.process_time()
list_3_enu=[]
for i,x in enumerate(list_1):
if x>450000:
y=list_2[i]
list_3_enu.append((x,y))
# measure time and cpu time of the task using enumerate
end_time=time.time()
end_cpu=time.process_time()
exec_time_enu=end_time-start_time
exec_cpu_enu=end_cpu-start_cpu
# print out number of elements in list 3
print("Number of elements in list 3 generated with
enumerate():",len(list_3_enu))
# print out elapsed time and cpu time for enumerate
print("Elapsed time with enumerate():",exec_time_enu)
print("CPU time with enumerate():",exec_cpu_enu)
# generate list 3 with tuples where first element is from list 1 and second element is from list 2 and every element is greater than 450000
start_time=time.time()
start_cpu=time.process_time()
list_3_zip=[(x,y) for x,y in zip(list_1,list_2) if x>450000]
# measure time and cpu time of the task using zip
end_time=time.time()
end_cpu=time.process_time()
exec_time_zip=end_time-start_time
exec_cpu_zip=end_cpu-start_cpu
# print out number of elements in list 3
print("Number of elements in list 3 generated with zip():",len(list_3_zip))
# print out elapsed time and cpu time for zip
print("Elapsed time with zip():",exec_time_zip)
print("CPU time with zip():",exec_cpu_zip)
# print out the difference between the cpu time when using enumerate and zip
diff_cpu=exec_cpu_enu-exec_cpu_zip
print("The difference between the CPU time when using enumerate and zip is:",diff_cpu)
What is code?
Code is a set of instructions written using a programming language to perform a specific task. It is a set of commands, functions, protocols, and objects that can be used to create a software program.
This code generates two lists with 1 million elements each by randomly generating integers between -5 million and 5 million (inclusive). Then, a third list is generated containing tuples consisting of elements from the first two lists, but only if they are greater than 450000. The code then measures the time and cpu time taken to create the third list. It first does it with enumerate(), and then with zip(). Finally, it prints out the elapsed time, cpu time, and the difference between the two.
To learn more about code
https://brainly.com/question/28818771
#SPJ4