31
loading...
This website collects cookies to deliver better user experience
i = 0
while i < 100_000_000:
i += 1
pass
for i in range(100_000_000):
pass
import timeit
def while_loop(n=100_000_000):
c = 0
t = 0
while(c<n):
t += c
c += 1
return t
def main():
print("while loop execution time => \t",
timeit.timeit(while_loop, number=1))
if __name__ == "__main__":
main()
Output:
while loop execution time => 13.3874618
def for_loop(n=100_000_000):
s = 0
for i in range(0, n):
s += i
return s
Output:
for loop execution time => 7.5061377
def for_loop_with_check_and_inc(n=100_000_000):
t = 0
c = 0
for _ in range(0, n):
if _ < n :
c += 1
t += _
return t
Output:
for loop execution time => 18.310060200000002
def sum_range_without_loop(n=100_000_000):
return sum(range(n))
Output:
sum method execution time => 4.478143
import numpy as np
def numpy_arange_sum(n=100_000_000):
return np.sum(np.arange(n))
numpy sum and arange execution time => 0.3317797