Is ruby suitable for a serious application?

I’ve come across the I/O problem while writing ruby codes. I had to load 15MB text file, and parse it. Writing I/O code is quite simple in ruby, but doing it fast is not. Here’s my ruby code.

#!/usr/local/bin/ruby -w
text = IO.readlines("window.txt")

Here’s my C++ counterpart.


using namespace std;

int main()
char buf[255];
fstream f(“window.txt”);
vector vs;

while (f.eof() == false)
f.getline(buf, 255);


Both of the codes do the same thing: load a file into an array. But the performance difference is remarkable. time test.rb shows me the following:

real 0m7.165s
user 0m6.092s
sys 0m1.072s

Interestingly enough, C++ is much faster than ruby not only in user space parts, but also in system call parts:

real 0m3.732s
user 0m3.474s
sys 0m0.250s

There are things I can do to make C++ program faster. I can think of more efficient vector instance declaration like

vector vs(130000);

because I know that the number of lines of the data file is about 130,000. But, I’m afraid that there might be no possible optimizations available in ruby case.

Similar Posts:

Post a Comment

Your email is never published nor shared.