Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from timeit import default_timer as timer
- # 1a. For-loop
- position1 = 0
- start = timer()
- for i in range(1, 10**10):
- end = timer()
- elapsedTimeInMilliseconds = 1000 * (end - start)
- if elapsedTimeInMilliseconds >= 1000:
- position1 = i
- break
- # 1b. Second for-loop
- position2 = 0
- start = timer()
- for i in range(1, 10**10):
- end = timer()
- elapsedTimeInMilliseconds = 1000 * (end - start)
- if elapsedTimeInMilliseconds >= 2000:
- position2 = i
- break
- # 2a. For-loop with conditions
- position3 = 0
- start = timer()
- for i in range(1, 10**10):
- if i < 0 or i > 10**10:
- print("Error")
- end = timer()
- elapsedTimeInMilliseconds = 1000 * (end - start)
- if elapsedTimeInMilliseconds >= 1000:
- position3 = i
- break
- # 2b. Second for-loop with conditions
- position4 = 0
- start = timer()
- for i in range(1, 10**10):
- if i < 0 or i > 10**10:
- print("Error")
- end = timer()
- elapsedTimeInMilliseconds = 1000 * (end - start)
- if elapsedTimeInMilliseconds >= 2000:
- position4 = i
- break
- # MAIN FUNCTION
- percentage1 = (position1 - position3) / position1
- percentage2 = (position2 - position4) / position2
- percentage1 = int(10000 * percentage1) / 10000
- percentage2 = int(10000 * percentage2) / 10000
- print("******** Execution time = 1 second ********")
- print(str(position1) + " simple iterations")
- print(str(position3) + " iterations with conditions (" + str(100*percentage1) + "% less)")
- print()
- print("******** Execution time = 2 seconds ********")
- print(str(position2) + " simple iterations")
- print(str(position4) + " iterations with conditions (" + str(100*percentage2) + "% less)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement