Rust vs. C++: performance and safety

2,273 views
Skip to first unread message

David Renshaw

unread,
Nov 17, 2013, 2:36:26 PM11/17/13
to capnproto
Hi everyone,
You may be interested in some posts I wrote comparing the C++ and Rust implementations of Cap'n Proto:

Kenton Varda

unread,
Nov 17, 2013, 6:33:17 PM11/17/13
to David Renshaw, capnproto
Nice!

It would be cool to make graphs with Python as well, for comparison.


--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
Visit this group at http://groups.google.com/group/capnproto.

Jason Paryani

unread,
Nov 17, 2013, 7:10:26 PM11/17/13
to capn...@googlegroups.com, David Renshaw
Alright, I've been meaning to do this for a while. Writing the benchmarks now.

Jason Paryani

unread,
Nov 18, 2013, 3:15:48 AM11/18/13
to capnproto
Well it seems I can't actually run capnproto-rust on my end. Does it require the latest rust-nightly (I'm only running v0.8)? For now, I've attached output from pycapnp's benchmarks, and here's a chart showing it off. As expected, python is about 20-50x slower than C++.
Inline image 1
carsales.png
out.txt

Andreas Stenius

unread,
Nov 18, 2013, 3:18:44 AM11/18/13
to capnproto
Me too! :)

Wouldn't be nice to have a common benchmark repo, kind of the capnp-test repo, that implements a framework for executing the benchmarks in various implementations and compiling the results in a comprehensible format. That way it would ease new implementations to join in.

2013/11/18 Jason Paryani <jpar...@gmail.com>

Kenton Varda

unread,
Nov 18, 2013, 3:34:35 AM11/18/13
to Jason Paryani, capnproto
Hmm, is this showing that protobuf with C extensions is actually slower than pure-python protobufs, or is the chart mislabeled?
carsales.png

Jason Paryani

unread,
Nov 18, 2013, 3:50:18 AM11/18/13
to Kenton Varda, capnproto
Nope, not mislabeled. The protobuf cpp implementation actually runs slower for carsales and catrank, and faster for eval. Also, protobuf runs faster for eval than pycapnp, but I imagine it's from Cap'n Proto using a union and the extra processing that calling which() and doing a string comparison adds.Inline image 2Inline image 1
catrank.png
carsales.png
eval.png

Kenton Varda

unread,
Nov 18, 2013, 3:59:53 AM11/18/13
to Jason Paryani, capnproto
Hmm, the difference between pycapnp and pycapnp_packed on catrank seems suspicious given that the packing is done entirely in C++.
eval.png
catrank.png
carsales.png

Jason Paryani

unread,
Nov 18, 2013, 4:22:30 AM11/18/13
to Kenton Varda, capnproto
First, I think there is a legitimate slowdown to using packed bytes in python, because I'm not doing it particularly well at the moment. For now, I'm using pipe to write the byte stream and then reading it back into a python string, all using python APIs. Beyond being slower, this could cause problems if a message went over 65KB. I'll be changing it in a bit.

Also, I changed the benchmarks to use a shared seed, so variance should be less of a problem now. I forgot to mention before, but I reduced iterations by 10x from the C++ benchmark, since it was taking forever for python to finish. I also changed from charting wall_time to user_time. Code is up at https://github.com/jparyani/pycapnp/tree/develop/benchmark if you want to check it out.Inline image 2Inline image 3Inline image 1



carsales.png
catrank.png
eval.png

Kenton Varda

unread,
Nov 18, 2013, 4:34:33 AM11/18/13
to Jason Paryani, capnproto
Oh, yeah, these benchmarks can vary a lot depending on the seed.  Actually, instead of using random.random(), you should probably copy the RNG code from the C++ benchmark.  It's pretty simple, and it will hopefully mean you generate exactly the same data in Python as is being generated in C++.  Not that it's going to make a significant difference in the Python vs. C++ comparison, obviously, but keeping it consistent across all the languages we benchmark would be good.

-Kenton
carsales.png
eval.png
catrank.png

David Renshaw

unread,
Nov 18, 2013, 7:46:11 AM11/18/13
to Jason Paryani, capnproto
On Mon, Nov 18, 2013 at 3:15 AM, Jason Paryani <jpar...@gmail.com> wrote:
Well it seems I can't actually run capnproto-rust on my end. Does it require the latest rust-nightly (I'm only running v0.8)?

You can either use rust-nightly or you can checkout the master branch and build from source.

Jason Paryani

unread,
Nov 18, 2013, 4:25:07 PM11/18/13
to capn...@googlegroups.com
I fixed up my benchmarks to now look like the C++ Cap'n Proto benchmarks. I also have a script that will run any benchmarks that follow this form in https://github.com/jparyani/pycapnp/blob/develop/benchmark/bin/run_all.py, and dump all the results as json to stdout.

Also Kenton, I changed to using your rand function. It was a bit of a pain, since you're relying on uint32_t overflow, so I ended up just writing it in c/cython.

Kenton Varda

unread,
Nov 19, 2013, 12:10:31 AM11/19/13
to Jason Paryani, capnproto
On Mon, Nov 18, 2013 at 1:25 PM, Jason Paryani <jpar...@gmail.com> wrote:
I fixed up my benchmarks to now look like the C++ Cap'n Proto benchmarks. I also have a script that will run any benchmarks that follow this form in https://github.com/jparyani/pycapnp/blob/develop/benchmark/bin/run_all.py, and dump all the results as json to stdout.

Nice work!
 
Also Kenton, I changed to using your rand function. It was a bit of a pain, since you're relying on uint32_t overflow, so I ended up just writing it in c/cython.

Can't you just write something like "n % (2 ** 32)" to implement modulo arithmetic?
Reply all
Reply to author
Forward
0 new messages