Hello, I write a simple lru code in rust and go. but the performance have a big gap

Hello, for i am a beginner in rust, and can someone should me what wrong with my rust code to case such a low performance compare with go

the code show here https://github.com/geniousli/point24/blob/master/README.md

time rust: target/debug/point24 3.79s user 0.05s system 97% cpu 3.931 total
time go: ./lru 0.04s user 0.01s system 77% cpu 0.062 total

First, you should compile for release, not debug.

Second, your implementation using Rc<Refcell<... is quite heavy. Look at the lru-cache crate and see how it's using a linked hashmap to do this efficiently. At least, you could try benchmarking those crates as a comparison to your implementation.

1 Like

Also you can get rid of a level of unnecessary indirection by removing all uses of Box.

thanks for yours replay, and i will have a try. your advise is important for a me. Thanks

For the reason, code should equal as implement in Go, so i used box

thank again. after i build a release
time: target/release/point24 0.03s user 0.01s system 70% cpu 0.044 total
and thank for lru-cache advise, for a depper research

Then your rust implementation is missing the garbage collector if it needs to be equivalent...

If you want to compare code in different languages follow the respective idioms and then do performance and readability benchmarks.

Do not try to enforce style of the original implementing language into the language you want to compare. That will almost always give results in favor of the original language.

5 Likes

Thanks