Comparing Ruby, Swift, and Crystal for processing a big file
I have been working on a thing that deals with stock market data. The format for the data is in CSV and, since I am using second-resolution data, they are big files. I am using Ruby, and I got curious about how would other languages compare in performance terms. I am learning Swift these days, and I have heard great things about Crystal, so I went ahead and wrote a performance test with these languages.
The results were:
Language | Version | Time |
---|---|---|
Ruby | 2.4.0 | 0.53s |
Crystal | 0.22.0 | 0.088s |
Swift | 3.1 | 1.38s |
In both Crystal and Ruby, utilizing File.foreach
and splitting the lines manually is like 3 times faster, compared to using the official CSV
libraries. Of course, these libraries implement a much more robust parsing, but it’s something good to keep in mind if your CSV is simple and just splitting on commas will do the job.
In Swift arrays are passed by value by default (they are structs), so I made sure to pass them by reference. After profiling the code I discovered that splitting the string and converting strings into doubles were, by far, the most expensive operations. They account for over 80% of the total execution time. This was my first experience with Instruments and it was really positive.
This isn’t a benchmark you can use to compare these languages, Swift is much faster than Ruby in general, but I was expecting Swift to be much faster than Ruby in this case. I am a newbie with the language, so it’s possible I am missing some obvious optimization. On the other hand, Crystal was very fast: seven times faster than Ruby!