From: Yohanes Santoso Date: 2002-06-20T15:54:06+09:00 Subject: Re: Why is I/O slow? Clifford Heath writes: > I gotta say folk, this is going to make it *very* hard to convince others > here to use ruby, and that's a shame... Well, I was intrigue by this, so I tried some of my own: jenny:~/tmp> time cat one_gig > /dev/null 0.090u 5.100s 0:53.85 9.6% 0+0k 0+0io 122pf+0w 0.050u 5.310s 0:54.18 9.8% 0+0k 0+0io 122pf+0w 0.120u 5.090s 0:54.34 9.5% 0+0k 0+0io 122pf+0w jenny:~/tmp> time ./cat.rb one_gig > /dev/null # Using IO#read with block size 8192 bytes 114.530u 6.810s 2:39.15 76.2% 0+0k 0+0io 338pf+0w 114.560u 6.590s 2:41.99 74.7% 0+0k 0+0io 338pf+0w 113.600u 6.840s 2:31.38 79.5% 0+0k 0+0io 336pf+0w # Using IO#sysread with block size 8192 bytes 1.030u 10.940s 0:51.41 23.2% 0+0k 0+0io 343pf+0w 0.950u 11.590s 0:50.36 24.9% 0+0k 0+0io 343pf+0w 0.920u 11.860s 0:51.41 24.8% 0+0k 0+0io 343pf+0w # Using IO#read with block size 16384 bytes 113.530u 6.670s 2:30.63 79.7% 0+0k 0+0io 336pf+0w 114.390u 6.370s 2:43.17 74.0% 0+0k 0+0io 336pf+0w 114.460u 7.340s 2:44.17 74.1% 0+0k 0+0io 336pf+0w # Using IO#sysread with block size 16384 bytes 0.810u 10.570s 0:50.25 22.6% 0+0k 0+0io 343pf+0w 0.670u 10.240s 0:50.04 21.8% 0+0k 0+0io 343pf+0w 0.850u 10.720s 0:50.26 23.0% 0+0k 0+0io 343pf+0w As can be seen, ruby's version could be faster than cat. When comparing things, it is important to compare apples with apples. cat uses the read syscall (man 2 read), so, use IO#sysread instead of IO#read. But by using sysread, you'll miss the niceness of readline, etc. CPU usage in ruby's sysread test is also higher than cat. But this illustrates the overhead of ruby compared to compiled c. IO#read reads a character at a time using getc (man 3 getc), that's why it's so slow. > *30* times slower than Perl?!! Your benchmark shows that perl's version is slower than cat. But ruby's version is faster than cat. So, perl's IO is actually slower than ruby's. This is nice. YS.