Commit 030e1e5
authored
⚡️ Speed up function
Here is an optimized version of your program.
Key improvements.
- Remove unnecessary comment and assignment for `j` (since you said the value/variable should be retained, I keep its assignment but comment on it).
- Limit object creation by using a tuple as the cache key (already done, since `lru_cache` sees the `number` parameter as hashable).
- `map(str, range(number))` is already fast; however, for even better runtime, join over a list comprehension (`list comprehension` is generally slightly faster than `map(str, ...)` in Python ≥3.7 due to interpreter optimizations) and remove the `min` from the cache by doing it outside (as soon as possible in `funcA`).
- Avoid repeated computation of `min(1000, number)` in the cache decorator.
**Why this is faster:**
- The use of list comprehension is usually a bit faster with primitive types.
- The unnecessary computation of `min()` is done outside of the `lru_cache`, reducing redundant cache keys and lookups.
- Kept your unused assignment as per your requirements.
If you want maximum throughput and the `number` argument is always a non-negative integer, this is about as fast as you can get using pure Python and `lru_cache`. (For huge-scale performance, a C-extension or writing directly to a buffer would be the next step, but is unnecessary here.)funcA by 6%1 parent fd9eb86 commit 030e1e5
File tree
1 file changed
+3
-5
lines changed- code_to_optimize/code_directories/simple_tracer_e2e
1 file changed
+3
-5
lines changedLines changed: 3 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
| 7 | + | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
| |||
63 | 61 | | |
64 | 62 | | |
65 | 63 | | |
66 | | - | |
67 | | - | |
| 64 | + | |
| 65 | + | |
68 | 66 | | |
69 | 67 | | |
70 | 68 | | |
| |||
0 commit comments