Java vs. Perl vs

The “Use Perl” website (use.perl.org) has a small entry commenting on a Java
vs. Perl test for performing string processing. The Perl site [1] says the
Java site [2] shows the Java source code for the match-up, but not the Perl
source. After some requests, the Java author released the Perl code to the
Perl folks (though it does not seem to have made it to the original Java
site). It’s on the Perl site [1].

Not surprisingly, the Java site [2] claims the Java code is better, as it
ran the test case faster.
Also not surprisingly, the Perl folks believe the Perl code is really not
quality code and can be optimized to do better than Java.

Just thought I’d mention it …

James

[1] http://use.perl.org/article.pl?sid=02/09/16/1448246
[2] http://developer.java.sun.com/developer/qow/archive/184/index.jsp

There are plenty of comments about the merits of the particular
benchmark, but I was interested to see how Ruby stacks up on my machine.
SO using Ruby 1.7.3 and Sun’s Linux 1.4.0 JDK I tried the code on the
Java site (plus the missing ") against

#!/usr/bin/env ruby

begin

unless ARGV.length == 2
    puts "Usage: #{$0} inputFile outputFile"
    exit 0
end

start_time = Time.now

File.open(ARGV[1], 'w') { |out|
    File.open(ARGV[0]).each_line { |line|
        unless ( line =~ /\.(?:css|jpg|gif|CSS|JPG|GIF) HTTP/ or
                 line =~ /^192\.(9|18|29)\./)
            out.print(line)
        end
    }
}

end_time = Time.now

puts "Processing time: #{end_time - start_time} Seconds"

rescue => message
puts “Error: #{message}”

end

[mike@ratdog tmp]$ time ruby CleanLogs.rb et-access.log xxx
Processing time: 6.579613 Seconds
2.88user 0.16system 0:06.82elapsed 44%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (256major+431minor)pagefaults 0swaps
[mike@ratdog tmp]$ time java CleanLogs et-access.log yyy
Processing Time: 7 Seconds
4.07user 0.39system 0:10.45elapsed 42%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (1631major+1110minor)pagefaults 0swaps

The outputs were identical, but as it was a quick between breakfast and
walking the dog hack I may have missed something.

:slight_smile:

Mike

···

In article CIELJOOMCFBDNHLICOEFMEDKCIAA.james@jamesbritt.com, JamesBritt wrote:

The “Use Perl” website (use.perl.org) has a small entry commenting on a Java
vs. Perl test for performing string processing. The Perl site [1] says the
Java site [2] shows the Java source code for the match-up, but not the Perl
source. After some requests, the Java author released the Perl code to the
Perl folks (though it does not seem to have made it to the original Java
site). It’s on the Perl site [1].

Not surprisingly, the Java site [2] claims the Java code is better, as it
ran the test case faster.
Also not surprisingly, the Perl folks believe the Perl code is really not
quality code and can be optimized to do better than Java.


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

JamesBritt wrote:

The “Use Perl” website (use.perl.org) has a small entry commenting on a Java
vs. Perl test for performing string processing. The Perl site [1] says the
Java site [2] shows the Java source code for the match-up, but not the Perl
source. After some requests, the Java author released the Perl code to the
Perl folks (though it does not seem to have made it to the original Java
site). It’s on the Perl site [1].

Not surprisingly, the Java site [2] claims the Java code is better, as it
ran the test case faster.
Also not surprisingly, the Perl folks believe the Perl code is really not
quality code and can be optimized to do better than Java.

Just thought I’d mention it …

Quick followup…

Someone sent in an optimized version of the Perl code. It turned out to
be twice as fast as the (current) Java code. Did anyone submit the Ruby
equivalent?

Regards,

Dan

File.open(ARGV[1], 'w') { |out|
    File.open(ARGV[0]).each_line { |line|
        unless ( line =~ /\.(?:css|jpg|gif|CSS|JPG|GIF) HTTP/ or
                 line =~ /^192\.(9|18|29)\./)
            out.print(line)
        end
    }
}

This one might be a little faster:

r1 = Regexp.new(/.(?:css|jpg|gif|CSS|JPG|GIF) HTTP/)
r2 = Regexp.new(/^192.(9|18|29)./)
File.open(ARGV[1], ‘w’) { |out|
File.open(ARGV[0]).each_line { |line|
next if r1.match(line) or r2.match(line)
out.print(line)
}
}

using strscan may give it another boost and
running under ruby 1.7 may be faster still.

I don’t have java installed to compare the original…

-joe