-
Notifications
You must be signed in to change notification settings - Fork 522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
String concatenation in a loop slow compared to Python #559
Comments
@arshajii thanks for help |
After some researching it seems that Python string objects have an optimization where they check if their reference count is 1 before concatenation, and if so they do a It's not as simple in Codon since we don't keep reference counts (instead we use a GC). It should be possible to still determine if an object has just a single reference via the GC, but we would need to do benchmarking to make sure it doesn't introduce performance problems in other scenarios. In the meantime, there is an internal string buffer type def test_string_concatenation_performance(num_entries: int) -> float:
start_time = time.time()
result = _strbuf() # <-- use strbuf object
for i in range(num_entries):
result.append(f"Number {i}: {i * 2}\n")
result = str(result) # <-- convert strbuf to string
return time.time() - start_time I'll keep this issue open until we figure out a general solution. |
@arshajii add for example:
How about this solution? but |
Does it work if you do:
I think it should work. Let me know if it does not. |
test code
codon test run
python3 test run
The text was updated successfully, but these errors were encountered: