|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """ |
3 | | -Benchmark comparisons for three methods to obtain the K smallest items from a list, |
| 3 | +Benchmark comparisons for four methods to obtain the K smallest items from a list, |
4 | 4 | for various values of K with different list sizes N (varying from 1,000 to 1,000,000). |
5 | 5 |
|
6 | 6 | For each method and each chosen K (as a percentage of N), the test is run 5 times |
|
10 | 10 | 1. Using built‐in sort: sort the list and slice the first K elements. |
11 | 11 | 2. Using heapq.nsmallest: use the heap‐based algorithm. |
12 | 12 | 3. Using quickselect: partition the list with selectlib.quickselect and slice the first K elements. |
| 13 | + 4. Using heapselect: partition the list with selectlib.heapselect and slice the first K elements. |
13 | 14 |
|
14 | 15 | The benchmark results are then plotted as grouped bar charts (one per N value) in a vertical stack. |
15 | | -Note: The percentages for K are now 0.1%, 1%, 10%, and 25% of N. |
| 16 | +Note: The percentages for K are now 0.2%, 1%, 10%, and 25% of N. |
16 | 17 | """ |
17 | 18 |
|
18 | 19 | import random |
@@ -40,17 +41,28 @@ def bench_quickselect(values, K): |
40 | 41 | is in the correct sorted position; then sort and return the first K elements. |
41 | 42 | """ |
42 | 43 | lst = values.copy() |
43 | | - # Partition in-place so that the element at index (K-1) is in the correct position. |
| 44 | + # Partition in-place so that the element at index (K-1) is in the correct position |
44 | 45 | selectlib.quickselect(lst, K - 1) |
45 | 46 | result = lst[:K] |
46 | 47 | result.sort() |
47 | 48 | return result |
48 | 49 |
|
| 50 | +def bench_heapselect(values, K): |
| 51 | + """ |
| 52 | + Use selectlib.heapselect on a copy of the list to partition it so that the element at index K-1 |
| 53 | + is in the correct sorted position; then sort and return the first K elements. |
| 54 | + """ |
| 55 | + lst = values.copy() |
| 56 | + # Partition in-place so that the element at index (K-1) is in the correct position. |
| 57 | + selectlib.heapselect(lst, K - 1) |
| 58 | + return lst[:K] |
| 59 | + |
49 | 60 | # List of methods to benchmark |
50 | 61 | methods = { |
51 | 62 | "sort": bench_sort, |
52 | 63 | "heapq.nsmallest": bench_heapq, |
53 | 64 | "quickselect": bench_quickselect, |
| 65 | + "heapselect": bench_heapselect, |
54 | 66 | } |
55 | 67 |
|
56 | 68 | def run_benchmarks(): |
@@ -111,11 +123,13 @@ def plot_results(overall_results): |
111 | 123 | "sort": -bar_width, |
112 | 124 | "heapq.nsmallest": 0, |
113 | 125 | "quickselect": bar_width, |
| 126 | + "heapselect": bar_width*2, |
114 | 127 | } |
115 | 128 | method_colors = { |
116 | 129 | "sort": '#1f77b4', |
117 | 130 | "heapq.nsmallest": '#ff7f0e', |
118 | | - "quickselect": '#2ca02c' |
| 131 | + "quickselect": '#2ca02c', |
| 132 | + "heapselect": '#d62728' |
119 | 133 | } |
120 | 134 |
|
121 | 135 | # Sort the overall_results by N for proper ordering (smallest to largest) |
|
0 commit comments