Commit 8c3010f
authored
⚡️ Speed up function
Here is a faster version of your program. The current code converts each integer to a string individually using map and then joins them. This can be improved by.
- Using an explicit list comprehension with preallocation is not much faster here.
- However, since all numbers are from 0 up to (number-1), Python's internal `" ".join()` with map is already very efficient.
- For small fixed max size (as in lru_cache(maxsize=1001)), you can precompute the strings up to that number and fetch them directly, reducing both time and memory for repetitive access.
But keeping the same signature and caching behavior, here's a further optimized version using a single global list to store precomputed results, which is faster than lru_cache with repeated access patterns (and fully thread-safe for read-only access).
This version will be significantly faster for numbers from 0 to 1000, which matches the intent of the original lru_cache(maxsize=1001), and is just as correct for larger (non-cached) values. The memory overhead is similar to lru_cache at its maximum, but with much lower per-call computational cost for the most common cases.
**Comments are unchanged unless relevant to the modification.**_cached_joined by 15,102%1 parent 0e4b548 commit 8c3010f
File tree
1 file changed
+7
-3
lines changed- code_to_optimize/code_directories/simple_tracer_e2e
1 file changed
+7
-3
lines changedLines changed: 7 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
58 | 57 | | |
59 | 58 | | |
60 | 59 | | |
61 | | - | |
62 | 60 | | |
63 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
0 commit comments