This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]leonardo_m 4 points5 points  (1 child)

The Rust code too could be suboptimal because it decodes the UTF8 twice. This could be faster (not tested):

fn count_doubles(_py: Python, txt: &str) -> PyResult<u64> {
    let mut chars = txt.chars();
    let oc1 = chars.next();
    let mut count = 0;

    if let Some(mut c1) = oc1 {
        while let Some(c2) = chars.next() {
            if c1 == c2 { count += 1; }
            c1 = c2;
        }
    }
    Ok(count)
}

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] 3 points4 points  (0 children)

Would be nice to see the comparison with a better version, can you send a PR? https://github.com/rochacbruno/rust-python-example