Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import timeit
- import math
- def binary_search(arr, item):
- low = 0
- high = len(arr)-1
- while low <= high:
- mid = math.floor((low + high) / 2)
- value = arr[mid]
- if value == item:
- return mid
- elif value > item:
- high = mid - 1
- else:
- low = mid + 1
- return None
- max_item_count = 1_000_000_000
- arr = [i for i in range(max_item_count)]
- item = 5
- t1 = timeit.timeit('binary_search(arr, item)', number=1, globals=globals())
- print(f'{t1}')
- time python3 ./binary_search.py
- 0.00040091900154948235
- 27.4 GB RAM USED
- real 7m39,021s
- user 0m35,848s
- sys 7m1,035s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement