Commit d7cbcd7
authored
Certainly! Let's optimize the function.
- The major cost here is calculating `int(0.75 * len(s))`.
- Multiplying floating-point numbers and casting to int is a minor but measurable cost in a tight loop.
- Instead, use integer multiplication and floor division to avoid float arithmetic. (i.e., `len(s) * 3 // 4`)
- Slicing cost is minimal and can't be improved.
- No need to further optimize or use extra imports as slicing is already a C-level operation.
**Optimized Code:**
This approach completely eliminates the float multiplication and the int cast, making it faster, especially when called many times. The semantics are unchanged for all practical string lengths.
1 parent 33c8258 commit d7cbcd7
1 file changed
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| |||
0 commit comments