absurd
6/27/2006 4:45:00 AM
> Robert Klemme wrote:
> > Nan Li wrote:
> > > Hello,
> > > I am relatively new to both ruby and perl. I like a lot about ruby.
> > > But I found ruby is about 5 - 8 times slower than perl when it comes
> > > to large text processing. I don't know if this is a well known fact or
> > > it just happens to me.
> >
> > It's known to be slower although I'd doubt the factor you mentioned.
> > What piece of code did you benchmark?
> >
> > Kind regards
> >
> > robert
Kenosis wrote:
> I concur. Please post your code so we can have a look. There are few
> key got-cha's you need to look out for. Also, you could try re-bench
> marking with YARV to see if that makes any significant difference in
> your case.
>
> Ken
>
Here is how I did my test:
I have 3 files:
1) genLog.pl
my $key = 'Start Start Start Start';
my @s = ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz' );
for ( $i =0; $i < 1024 * 1024; $i++ ) {
print $key, "\n";
foreach ( @s ) {
print $_, "\n";
}
}
2) test.pl
my $log = 'log';
my @block = ();
open( FD, $log );
while( <FD> ) {
chomp;
if ( m/Start Start Start Start/ ) {
push @block, $_;
}
}
print scalar @block, "\n";
3) test.rb
log = 'log'
block = []
File.open( log ) { |f|
f.each_line { |line|
line.chomp!
if ( line =~ /Start Start Start Start/ ) then
block << line
end
}
}
puts block.size
I used genLog.pl to generate a large text file, and then time test.pl
and test.rb
My test ran as belows:
[nan@athena test]$ perl genLog.pl > log
[nan@athena test]$ ls -lh log
-rw-rw-r-- 1 nan nan 78M Jun 27 00:25 log
[nan@athena test]$ time perl test.pl
1048576
real 0m3.469s
user 0m3.252s
sys 0m0.136s
[nan@athena test]$ time ruby test.rb
1048576
real 0m18.775s
user 0m16.525s
sys 0m0.336s
ruby program is about 6 times slower. The above 2 scripts use the
same language constructs, the same algorithm. The problem lies in the
language itself or the implementation of the language.