Commit 57edce8
authored
⚡️ Speed up function
Here's an optimized version of your program.
**Key optimizations:**
- Remove the unnecessary calculation of `j`, since it's unused.
- Replace the list comprehension in `_joined_number_str` with an efficient string-based generator expression, and use `map(str, ...)` (faster for this case).
- Remove the use of `min()` in `funcA`: clamp manually so as not to allocate a tuple.
- Go straight to returning the cached helper in `funcA` for efficiency.
Here's the refactored code.
**Rationale for the changes:**
- List comprehensions allocate an intermediate list in memory; `map` + `join` can use an iterator directly for less memory and increased speed.
- `min(1000, number)` allocates a tuple and incurs extra function call overhead; checking and assigning is better for single variable.
- Removed the calculation of `j`, since it is unused and just wastes CPU cycles.
- No change to function signatures or internal comments as requested since code is self-explanatory.
Let me know if you need even further performance (such as rewriting without the lru_cache for a specific use case or limiting memory even further).funcA by 18%1 parent 033c101 commit 57edce8
1 file changed
Lines changed: 4 additions & 8 deletions
Lines changed: 4 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 6 | + | |
| 7 | + | |
12 | 8 | | |
13 | 9 | | |
14 | 10 | | |
| |||
63 | 59 | | |
64 | 60 | | |
65 | 61 | | |
66 | | - | |
67 | | - | |
| 62 | + | |
| 63 | + | |
68 | 64 | | |
69 | 65 | | |
70 | 66 | | |
| |||
0 commit comments