Skip to content

Commit 19e1b47

Browse files
heikkitoivonencodex
andcommitted
Update: Stabilize timing tests
Co-Authored-By: Codex <codex@openai.com>
1 parent 5514ea4 commit 19e1b47

8 files changed

Lines changed: 231 additions & 249 deletions

docs/stdlib/queue.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ lifo.put('c') # O(1)
118118

119119
# Pop items (last in, first out) - O(1) amortized
120120
print(lifo.get()) # O(1) - 'c'
121+
print(lifo.get()) # O(1) - 'b'
121122
print(lifo.get()) # O(1) - 'a'
122123
```
123124

scripts/audit_documentation.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,19 @@ def generate_audit_report(workspace_root: Path) -> dict[str, Any]:
137137
"builtins": {
138138
"total": len(all_builtins),
139139
"documented": len(documented["builtins"]),
140-
"coverage_percent": round(
141-
100 * len(documented["builtins"]) / len(all_builtins), 1
142-
),
140+
"coverage_percent": round(100 * len(documented["builtins"]) / len(all_builtins), 1),
143141
"missing": sorted(missing_builtins),
144142
"by_category": builtins_by_category,
145143
},
146144
"stdlib": {
147145
"total": len(stdlib_modules),
148146
"documented": len(documented["stdlib"]),
149-
"coverage_percent": round(
150-
100 * len(documented["stdlib"]) / len(stdlib_modules), 1
151-
),
147+
"coverage_percent": round(100 * len(documented["stdlib"]) / len(stdlib_modules), 1),
152148
"missing": missing_stdlib,
153149
},
154150
"summary": {
155151
"total_items": len(all_builtins) + len(stdlib_modules),
156-
"total_documented": len(documented["builtins"])
157-
+ len(documented["stdlib"]),
152+
"total_documented": len(documented["builtins"]) + len(documented["stdlib"]),
158153
"overall_coverage_percent": round(
159154
100
160155
* (len(documented["builtins"]) + len(documented["stdlib"]))
@@ -195,9 +190,7 @@ def print_report(report: dict[str, Any]) -> None:
195190
for item in report["builtins"]["missing"][:20]: # Show first 20
196191
print(f" - {item}")
197192
if len(report["builtins"]["missing"]) > 20:
198-
print(
199-
f" ... and {len(report['builtins']['missing']) - 20} more"
200-
)
193+
print(f" ... and {len(report['builtins']['missing']) - 20} more")
201194

202195
print("\n📚 STDLIB MODULES")
203196
print(f" Total: {report['stdlib']['total']}")

scripts/estimate_complexity.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
python scripts/estimate_complexity.py my_script my_sorting_function
1313
"""
1414

15-
import sys
16-
import time
1715
import importlib
16+
import inspect
1817
import math
1918
import statistics
20-
import inspect
19+
import sys
20+
import time
2121
import typing
2222
from pathlib import Path
2323

@@ -31,44 +31,45 @@ def measure_execution_time(func, input_size, iterations=5):
3131
Uses type hints to determine whether to pass 'n' (int) or data of size 'n'.
3232
"""
3333
input_data = None
34-
34+
3535
# 1. Check type hints
3636
try:
3737
sig = inspect.signature(func)
3838
params = list(sig.parameters.values())
3939
if params:
4040
first_param = params[0]
4141
hint = first_param.annotation
42-
42+
4343
if hint is int:
4444
input_data = input_size
45-
elif hint in (list, typing.List, typing.Sequence):
45+
elif hint in (list, typing.Sequence):
4646
# Simple list generation
4747
input_data = list(range(input_size))
4848
# Handle generic aliases like list[int] in newer Python
49-
elif hasattr(hint, "__origin__") and hint.__origin__ in (list, typing.List, typing.Sequence):
50-
input_data = list(range(input_size))
49+
elif hasattr(hint, "__origin__") and hint.__origin__ in (list, typing.Sequence):
50+
input_data = list(range(input_size))
5151
except (ValueError, TypeError):
5252
# Signature inspection failed or function is weird
5353
pass
5454

5555
# 2. Heuristic fallback logic
5656
if input_data is None:
5757
return _measure_heuristic(func, input_size, iterations)
58-
58+
5959
# 3. Execution with determined input
6060
try:
6161
start_time = time.perf_counter()
6262
for _ in range(iterations):
6363
func(input_data)
6464
end_time = time.perf_counter()
6565
return (end_time - start_time) / iterations
66-
except Exception as e:
67-
# If specific input failed, maybe try heuristic as last resort?
66+
except Exception:
67+
# If specific input failed, maybe try heuristic as last resort?
6868
# But for now, just report error to avoid infinite fallback loops.
6969
# print(f"Error with generated input: {e}")
7070
return None
7171

72+
7273
def _measure_heuristic(func, input_size, iterations):
7374
"""Fallback: Try int first, then list."""
7475
try:
@@ -89,6 +90,7 @@ def _measure_heuristic(func, input_size, iterations):
8990
except Exception:
9091
return None
9192

93+
9294
def detect_complexity(n_values, times):
9395
"""
9496
Estimate complexity by comparing RSquared values for different models.
@@ -99,40 +101,42 @@ def detect_complexity(n_values, times):
99101

100102
# Normalize times
101103
min_time = min(times)
102-
if min_time == 0: min_time = 1e-9
104+
if min_time == 0:
105+
min_time = 1e-9
103106
normalized_times = [t / min_time for t in times]
104-
107+
105108
models = {
106109
"O(1) (Constant)": [1 for _ in n_values],
107110
"O(log n) (Logarithmic)": [math.log(n) if n > 0 else 0 for n in n_values],
108-
"O(n) (Linear)": [n for n in n_values],
111+
"O(n) (Linear)": list(n_values),
109112
"O(n log n) (Linearithmic)": [n * math.log(n) if n > 0 else 0 for n in n_values],
110113
"O(n^2) (Quadratic)": [n**2 for n in n_values],
111114
}
112115

113116
best_fit = None
114-
best_score = -float('inf')
117+
best_score = -float("inf")
115118

116119
for name, theoretical in models.items():
117120
# Calculate correlation coefficient (Pearson)
118121
try:
119-
if len(set(theoretical)) == 1: # Handle constant case
122+
if len(set(theoretical)) == 1: # Handle constant case
120123
# For constant time, we check variance of times
121-
score = 1.0 / (statistics.stdev(normalized_times) + 1.0)
124+
score = 1.0 / (statistics.stdev(normalized_times) + 1.0)
122125
else:
123-
# Correlation between theoretical and actual
124-
# Using covariance / (std_dev_x * std_dev_y)
125-
correlation = statistics.correlation(theoretical, times)
126-
score = correlation
127-
126+
# Correlation between theoretical and actual
127+
# Using covariance / (std_dev_x * std_dev_y)
128+
correlation = statistics.correlation(theoretical, times)
129+
score = correlation
130+
128131
if score > best_score:
129132
best_score = score
130133
best_fit = name
131134
except statistics.StatisticsError:
132135
continue
133-
136+
134137
return best_fit, best_score
135138

139+
136140
def main():
137141
if len(sys.argv) < 3:
138142
print(__doc__)
@@ -149,7 +153,7 @@ def main():
149153
sys.exit(1)
150154

151155
print(f"Estimating complexity for {module_name}.{func_name}...")
152-
156+
153157
# Input sizes to test
154158
n_values = [100, 500, 1000, 2000, 5000]
155159
times = []
@@ -170,6 +174,7 @@ def main():
170174
print("-" * 35)
171175
print(f"Estimated Complexity: {complexity}")
172176
print(f"Fit Score: {score:.3f}")
173-
177+
178+
174179
if __name__ == "__main__":
175180
main()

0 commit comments

Comments
 (0)