You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
⚡️ Speed up function encode_str by 40% in PR #224 (remove-tiktoken)
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.
0 commit comments