On 11/3/06, Bryan Sant <[EMAIL PROTECTED]> 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("match");
    else
      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
done

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)>[EMAIL PROTECTED] and $_="not a $_"}' < five-mill.txt 
>
/dev/null

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

Use the right tool for the job I suppose.

-Bryan

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to