Commit 0f825dc
authored
⚡️ Speed up function
Here’s an optimized version of your program.
**Key improvements:**
- Replace `map(str, range(number))` with a list comprehension and use `" ".join(str(i) for i in range(number))`. In CPython 3.11, both are close in speed, but since you want *maximum speed* and minimal memory per call, it actually pays off to use a preallocated list of strings (avoiding the generator overhead).
- Switching the cache from `functools.lru_cache` to a simple fixed-size array cache (since the range is known and contiguous) yields a major speedup—O(1) lookup with no hashing.
- Avoid all global per-call function overheads by not using decorators.
- If you want to preserve the exact signature and decorator-based cache, skip the array cache.
Here’s the faster version:
**Notes:**
- This eliminates LRU cache overhead (hashing, dict lookup, reference management).
- For all subsequent calls, lookup is instant: `O(1)` list access.
- List comprehension is slightly faster than `map` due to the avoidance of Python-level function calls per element.
**If you must preserve the use of @lru_cache for API compatibility:**
Your code is already quite optimal. Minor tweak (list comprehension).
But the first method above is substantially faster/more memory efficient if your input domain is tightly bounded as assumed._cached_joined by 8%1 parent 75fb4d4 commit 0f825dc
File tree
1 file changed
+3
-3
lines changed- code_to_optimize/code_directories/simple_tracer_e2e
1 file changed
+3
-3
lines changedLines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | | - | |
69 | | - | |
| 68 | + | |
| 69 | + | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| |||
0 commit comments