Commit d4abafa
authored
⚡️ Speed up function
Certainly! Here are the main efficiency aspects for this code.
- The only real computation is converting a sequence of numbers (from 0 to n-1) into a space-separated string.
- `lru_cache` is already helping for repeated calls with the same argument, so further internal caching won't help much.
- However, `str.join(map(str, range(n)))` can still be optimized for Python (especially for big n), since `" ".join(map(str, ...))` builds many intermediate strings.
#### Optimization Approach
The main optimization possible.
- Use a direct string concatenation method that's faster for large inputs, avoiding generator overhead.
- Pre-allocate and fill a list of all number strings, then join once.
However, `" ".join(...list of strings...)` is already quite fast and optimal in CPython, so the improvement is limited and mostly negligible unless used at very large n.
But we can do a bit better with.
- Use a faster conversion for small n (shortcut for n == 0, n == 1).
- Avoid map completely; list comprehension is just as fast or faster in Py3.
#### Additional Micro-optimization
- Avoid `min(number, 1000)` by using an early return if input number >= 1000 (helps with call overhead if called with huge numbers frequently).
Here's the rewritten code with faster runtime.
#### Changes.
- Early exits for n == 0 and n == 1 (eliminates unnecessary work for tiny inputs).
- Slightly faster list comprehension for string conversion.
- Reduced overhead in the conditional min.
- Preserved all original cache semantics and function signatures.
- No unnecessary `map` generator creation.
#### Result.
- Slightly lower runtime overhead, especially for small n and at scale.
- Lower memory overhead by not keeping an intermediate generator alive.
- The code remains simple and clean.
If you use `funcA` repeatedly with numbers 0–1000, this is about as fast as Python can go for this problem!
Let me know if you have a larger scale or further constraints.funcA by 12%1 parent 57edce8 commit d4abafa
1 file changed
Lines changed: 10 additions & 3 deletions
Lines changed: 10 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | | - | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | | - | |
63 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
64 | 71 | | |
65 | 72 | | |
66 | 73 | | |
| |||
0 commit comments