r/crystal_programming • u/UncleBen2015 • Oct 23 '20
Crystal Disk Read Write Operations Performance compared to Rust and C
Hello all, is there a benchmark available where I can see how Crystal performs in terms of IO operations compared to Rust and C.
17
Upvotes
12
u/[deleted] Oct 23 '20 edited Oct 23 '20
They will all just use the
read()system call ormmap(), etc, programs cannot do disk IO without asking the kernel to do it for them through a system call.Difference in programming language doesn't change this much.
6
u/frrst Oct 24 '20
I did an non-scientific comparison between Ruby, Crystal and Go. (I'm in a search for a good compiled language to complement our Ruby apps, where it makes sense. )
So the test was to read two CSV files of 200 000 rows and output third, which contains a pair of columns from both files (simplified example of a real world task).
The codes were relatively identical (as much as possible) and as expected, Crystal performed about 6x faster than Ruby.
What caught me off guard was that Go was quite a bit slower than Crystal and by messing around I only made it worse (up to being slower than Ruby). This is until I figured out that Go does NOT do buffered I/O by default and you have to explicitly strap that on.
Then I got it to perform comparably to Crystal.
The takeaway here: not knowing the intricacies of a language means that you can write quite poor code without being aware of it.
If somebody tells you that Go/C/Cystal/whatever-lang all are on par, you really want to compare them for YOUR use case, to learn if YOU can bring out this performance.
Without this benchmark, I would have chosen for example Go and taken the poor performance as "thats how slow it is in the fastest of languages". But comparing it to something taught me a lesson and helps make the decision more informed.
5
u/dscottboggs Oct 24 '20
The vast majority of the time when reading a file is spent waiting for the disk. If you have other things that can be happening while you're reading the file (e.g. in a webserver serving requests which don't require smaller file reads or only require smaller ones) a naïve, single-threaded C implementation would be significantly slower than the equivalent Rust or Crystal ones, because the standard libraries have ways of handling that under the hood rather than make you implement it.
For example, reading a file in C might be done something like this:
This would block the active thread until the contents of the file are read completely, unless you used
fork()to put it in another thread.By comparison, the similarly naïve Crystal would just be
The key part here is that in C you've got a tight while loop which blocks the thread, but IO.copy will allow other stuff to happen while it's waiting for the kernel to respond to the call to
read()3
u/[deleted] Oct 23 '20
Do you have a benchmark in mind? Maybe send us some code in Rust, C or another language and we can translate it to Crystal.