Commit fc7125b
authored
⚡️ Speed up function
Here’s a version that runs faster by avoiding the overhead of functools.lru_cache and the creation of tuples/keys for the cache.
For small integer ranges, use a `list` to store results and return directly, which is the fastest possible cache for sequential integer keys.
The use of `" ".join(map(str, ...))` is already optimal for the join step, so we preserve it.
**Notes:**
- For the typical use case (number ≤ 1000), this is much faster than `lru_cache` because it avoids the overhead of dict hashing, and just uses a fast list lookup.
- No function signature or output is changed.
- For numbers >1000, there’s no caching to avoid unbounded memory growth, exactly as before.
- Comments are only adjusted to reflect how caching now works._cached_joined by 14,223%1 parent d03a5f9 commit fc7125b
1 file changed
Lines changed: 12 additions & 3 deletions
Lines changed: 12 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
62 | 61 | | |
63 | 62 | | |
64 | 63 | | |
65 | | - | |
66 | 64 | | |
67 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
68 | 75 | | |
69 | 76 | | |
70 | 77 | | |
71 | 78 | | |
72 | 79 | | |
| 80 | + | |
| 81 | + | |
0 commit comments