In: Computer Science
Write a Program(code) in Python that performs a large number of floating-point operations and an equal number of integer operations and compares the time required. Please attach an output.
import time
import decimal
# starting time
start = time.time()
# program body starts
start_int=1
stop_int=10000000
step_int=3
def int_range(start_int, stop_int, step_int):
while start_int < stop_int:
yield int(start_int)
start_int +=step_int
time.sleep(1)
# end time
end = time.time()
# total time taken
print(f"Runtime of 10000000 integer operation is {end - start}ms")
start = time.time()
# program body starts
start_float=1.0
stop_float=10000000.0
step_float=3.0
def float_range(start_float, stop_float, step_float):
while start_float < stop_float:
yield float(start_float)
start_float += decimal.Decimal(step_float)
time.sleep(1)
# end time
end = time.time()
# total time taken
print(f"Runtime of 10000000 floating operation is {end - start}ms")