$10K coding deathmatch

Bryan Sant bryan.sant at gmail.com
Fri Nov 3 14:27:25 MST 2006

On 11/3/06, Bryan Sant <bryan.sant at gmail.com> wrote:
> > Hmm, I could substitute Java for Lisp there and be just as accurate. :)
> Absolutely.
> -Bryan

Alright, just for fun, here is a Java submission.

import java.io.*;
public class Q1 {
  public static void main(String[] args) throws Exception {
   BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
   String line;
   while ((line = reader.readLine()) != null) {
     boolean match = true;
     String[] nums = line.split(" ");
     int len = nums.length - 1;
     for (int i = 0; i < len; i++) {
       int n = Integer.parseInt(nums[i]);
       int next = Integer.parseInt(nums[i + 1]);
       int diff = Math.abs(n - next);
       if (diff > len || diff == 0)
         match = false;
     if (match)
       System.out.println("not a match");

Compile with:  javac -g:none Q1.java
Run with:  java -Xverify:none Q1

How does she perform?

real    0m0.142s
user    0m0.076s
sys     0m0.020s

Here is the (fastest) Perl performance on my system:

real    0m0.014s
user    0m0.012s
sys     0m0.000s

I know, I should hang my head in shame.  When it comes to processing 5
lines of input, Java is an order of magnitude slower than perl (and
other scripting langs) :-(.

Now assuming that those five example lines exist in a file named
five.txt, let's do this:

cp five.txt five-mill.txt
for i in `seq 1 21`; do
  cat five-mill.txt >> five-mill.tmp
  cp five-mill.tmp five-mill.txt

Now we have a little over five million lines of input to process.
Let's rerun those programs:

time java -Xverify:none -server Q1 < five-mill.txt > /dev/null

real    0m28.665s
user    0m23.013s
sys     0m4.504s

time perl -ape '$_="match\n";$i=0;while(++$i<@F){last
if(abs$F[$i]-$F[$i-1]||@F)>=@F and $_="not a $_"}' < five-mill.txt >

real    1m7.020s
user    1m4.276s
sys     0m1.408s

Use the right tool for the job I suppose.


More information about the PLUG mailing list