Skip to content

Commit d7cbcd7

Browse files
⚡️ 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.
1 parent 33c8258 commit d7cbcd7

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
from codeflash.cli_cmds.console import logger
1212

13+
1314
def encode_str(s: str) -> str:
14-
return s[:int(0.75 * len(s))]
15+
# Use integer math for the index calculation to avoid float operations
16+
return s[: len(s) * 3 // 4]
17+
1518

1619
def get_qualified_name(module_name: str, full_qualified_name: str) -> str:
1720
if not full_qualified_name:

0 commit comments

Comments
 (0)