Skip to content

Commit e984169

Browse files
⚡️ Speed up function encode_str by 31% in PR #224 (remove-tiktoken)
Here’s an optimized version of your program. The current code repeatedly computes `int(0.75 * len(s))` and slices the string, which is already fast, but can be micro-optimized. - Avoid the float multiplication and casting by directly calculating `(len(s) * 3) // 4`, which is faster and avoids possible floating-point artifacts. Rewritten code. This version removes the float operation and is slightly faster, especially for large strings. The return value is exactly the same as before.
1 parent f5bfcd9 commit e984169

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+
end = (len(s) * 3) // 4 # use integer arithmetic for speed
16+
return s[:end]
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)