|
1 | | -# How to Do a Binary Search in Python? |
| 1 | +# How to Do a Binary Search in Python |
2 | 2 |
|
3 | | -Code snippets supplementing the [How to Do a Binary Search in Python?](https://realpython.com/binary-search-python/) article on [Real Python](https://realpython.com/). |
| 3 | +Code snippets supplementing the [How to Do a Binary Search in Python](https://realpython.com/binary-search-python/) article. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +### Download Sample Dataset |
| 8 | + |
| 9 | +To download a sample dataset for benchmarking, run the following script: |
| 10 | + |
| 11 | +```shell |
| 12 | +$ python download_imdb.py |
| 13 | +Fetching data from IMDb... |
| 14 | +Created "names.txt" and "sorted_names.txt" |
| 15 | +``` |
| 16 | + |
| 17 | +### Benchmark |
| 18 | + |
| 19 | +**Note:** The hash-based search algorithm requires a separate data structure, so it's not included here. Feel free to treat it as an exercise. |
| 20 | + |
| 21 | +Find the index of Arnold Schwarzenegger in unsorted names using random search: |
| 22 | + |
| 23 | +```shell |
| 24 | +$ python benchmark.py -a random -f names.txt 'Arnold Schwarzenegger' |
| 25 | +Loading names...ok |
| 26 | +[1/10] Found at index=215 (21.68 s) |
| 27 | +[2/10] Found at index=215 (148.10 ms) |
| 28 | +[3/10] Found at index=215 (63.49 s) |
| 29 | +[4/10] Found at index=215 (15.93 s) |
| 30 | +[5/10] Found at index=215 (23.38 s) |
| 31 | +[6/10] Found at index=215 (60.98 s) |
| 32 | +[7/10] Found at index=215 (10.14 s) |
| 33 | +[8/10] Found at index=215 (15.81 s) |
| 34 | +[9/10] Found at index=215 (5.10 s) |
| 35 | +[10/10] Found at index=215 (13.36 s) |
| 36 | +best=148.10 ms, worst=63.49 s, avg=23.00 s, median=15.87 s |
| 37 | +``` |
| 38 | + |
| 39 | +Find the index of Arnold Schwarzenegger in unsorted names using linear search: |
| 40 | + |
| 41 | +```shell |
| 42 | +$ python benchmark.py -a linear -f names.txt 'Arnold Schwarzenegger' |
| 43 | +Loading names...ok |
| 44 | +[1/10] Found at index=215 (30.95 µs) |
| 45 | +[2/10] Found at index=215 (26.86 µs) |
| 46 | +[3/10] Found at index=215 (26.26 µs) |
| 47 | +[4/10] Found at index=215 (26.55 µs) |
| 48 | +[5/10] Found at index=215 (26.24 µs) |
| 49 | +[6/10] Found at index=215 (25.88 µs) |
| 50 | +[7/10] Found at index=215 (25.77 µs) |
| 51 | +[8/10] Found at index=215 (25.89 µs) |
| 52 | +[9/10] Found at index=215 (25.91 µs) |
| 53 | +[10/10] Found at index=215 (25.99 µs) |
| 54 | +best=25.77 µs, worst=30.95 µs, avg=26.63 µs, median=26.11 µs |
| 55 | +``` |
| 56 | + |
| 57 | +Find the index of Arnold Schwarzenegger in *sorted* names using binary search: |
| 58 | + |
| 59 | +```shell |
| 60 | +$ python benchmark.py -a binary -f sorted_names.txt 'Arnold Schwarzenegger' |
| 61 | +Loading names...ok |
| 62 | +[1/10] Found at index=782431 (18.82 µs) |
| 63 | +[2/10] Found at index=782431 (9.19 µs) |
| 64 | +[3/10] Found at index=782431 (11.08 µs) |
| 65 | +[4/10] Found at index=782431 (9.70 µs) |
| 66 | +[5/10] Found at index=782431 (10.14 µs) |
| 67 | +[6/10] Found at index=782431 (9.35 µs) |
| 68 | +[7/10] Found at index=782431 (9.42 µs) |
| 69 | +[8/10] Found at index=782431 (8.79 µs) |
| 70 | +[9/10] Found at index=782431 (8.66 µs) |
| 71 | +[10/10] Found at index=782431 (8.79 µs) |
| 72 | +best=8.66 µs, worst=18.82 µs, avg=10.39 µs, median=9.38 µs |
| 73 | +``` |
0 commit comments