Why these codes is 8x slower than python?

Rust version

#[derive(Debug)]
struct Price {
    t: Vec<NaiveDateTime>,
    o: Vec<f32>,
    h: Vec<f32>,
    l: Vec<f32>,
    c: Vec<f32>,
    v: Vec<f32>,
    ct: Vec<String>,
}

impl Price {
    fn new() -> Self {
        Price {
            t: vec![],
            o: vec![],
            h: vec![],
            l: vec![],
            c: vec![],
            v: vec![],
            ct: vec![],
        }
    }
}

fn read_csv(path: &str) -> Price {
    let mut reader = csv::Reader::from_path(path).unwrap();
    let mut price = Price::new();
    let cols = reader.headers().unwrap().clone();
    for record in reader.records() {
        let record = record.unwrap();
        price.c.push(record[0].parse::<f32>().unwrap());
        price.ct.push(String::from(&record[2]));
        price.h.push(record[3].parse::<f32>().unwrap());
        price.l.push(record[4].parse::<f32>().unwrap());
        price.o.push(record[5].parse::<f32>().unwrap());
        price.t.push(NaiveDateTime::parse_from_str(&record[6], "%Y-%m-%dT%H:%M:%S%.f").unwrap());
        price.v.push(record[7].parse::<f32>().unwrap());
    }
    price
}

t!(let data = read_csv(path));
26.1554ms

Python version

@time CSV.read("../nb_factorw/test_csv.csv", DataFrame);
0.003885 seconds (4.28 k allocations: 3.419 MiB)

What am I missing here?

Did you run it in release mode?

I ran it on evcxr in opt3, which is almost the same as were been ran on relese mode in the experied I had.

Does the t! macro run the code at least twice taking the lower value? Ditto for the Python bench-marker. Did you happen to run the Rust version first?

There is also a lot of indexing and thus bounds checking in the Rust code. The vectors are not pre-allocated with a reasonable capacity, thus they need to reallocate quite often.

You should also check whether NaiveDateTime::parse_from_str is perhaps the bottleneck – for that, use a profiler.


I reckon this is not your first question of the form "Why is Rust slower than <Python code with no context>". This is not a helpful way to frame questions. If you are really curious why it's slower, then use a profiler and check it for yourself. If you are merely trying to subvert the question to say Rust is slow, then refrain from posting.

1 Like

Yes, t! run more than 1 time. I ran it in evcxr, and I thought the first time of running t! would count time of compiling. But by testing it in diffrent running time, I found out running times make no difference.

1 Like

Of course I am not just tring to say Rust is slow, which makes no sense to me, and no doubtly Rust is much faster than Python. I am just to lean the tricks to write fase Rust code. Profiling is fine, but for now I am just avoid writting some apparenmental "wrong" codes such as being in these simple code.
Thanks for your reply.

1 Like

I've seen people complain that evcxr doesn't use the release mode. Add to your code to verify:

debug_assert!(false);
5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.