DEV Community

Discussion on: Extending Python with Rust

Collapse
 
p_chhetri profile image
Pradeep Chhetri

Sure, I will try that out. Thank you for the suggestions.

Collapse
 
idkravitz profile image
Dmitry Kravtsov • Edited

In addition to my suggestion, you don't actually need to calculate sum(log10(i)). The idea is simple: the concatenation of numbers 0..9 is 10*1 chars in length, for 0..100 is 10*1 + 90*2 chars in length, for 0..1000 it is 10*1 + 90*2 + 900*3, and so on. So you can tell what your resulting string length almost in O(log10(n)) calculations, which is super fast.

Thread Thread
 
p_chhetri profile image
Pradeep Chhetri

Awesome thanks a lot Dmitry.

Thread Thread
 
idkravitz profile image
Dmitry Kravtsov

You're wellcome. One last thing - as far as I know Rust doesn't have a C-equivalent of itoa, in other words there is no function in Rust stdlib to convert number to string in preallocated buffer. But there is an itoa crate which does exactly that, you can read about it here: docs.rs/itoa/0.4.5/itoa/

I feel strangely excited about messing with these benchmarks, so I may share my results in a few days when I'll try these things.

Thread Thread
 
p_chhetri profile image
Pradeep Chhetri

After reading all your comments, i am very excited. I am new to rust but your feedback is very motivating. I'll work on implementing your feedbacks. Thank you very much. Looking forward to your benchmarks too.

Thread Thread
 
idkravitz profile image
Dmitry Kravtsov

So I implemented a single allocation version in Rust with itoa crate. The results exceeded my expectations:

Name (time in us) Mean
test_rust_concat_mutable 7.9843 (1.0)
test_rust_concat2 90.8439 (11.38)
test_rust_concat 92.4594 (11.58)
test_concat_comprehension 202.4457 (25.36)
test_concat_join 238.2960 (29.85)
test_concat_basic 360.8961 (45.20)

Mutable implementation can be found in this gist

Thread Thread
 
p_chhetri profile image
Pradeep Chhetri

Nice Dmitry, this looks awesome.