[Shootout-list] Two java submissions
James McIlree
ovrskeek@mac.com
Tue, 11 Jan 2005 01:14:12 -0800
--Apple-Mail-15-426117134
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed
As per the earlier email, could someone please submit these
to the CVS tree?
One of these is new (wordfreq.java), and the other is a repeat,
as I didn't know to request a CVS submission.
Something I noticed during a few quick timing runs, both of these
tests (and I'm guessing many of the others) are so short that the java
compilers aren't really having time to do much. Given the run time of
these tests, the client compiler is often better than the server
compiler.
Using the default test size (input.txt x 25) with wordfreq, the
client compiler was faster:
client - 1.093u 0.086s 0:01.26 90.4%
server - 1.236u 0.089s 0:01.39 94.2%
However, using a larger data set (input.txt x 625), to give a run
time that isn't dominated by startup and compilation:
client - 22.011u 0.452s 0:22.53 99.6%
server - 11.367u 0.475s 0:11.84 99.9%
That is a really substantial difference, that isn't being shown by
the tests as they are run now.
James M
--Apple-Mail-15-426117134
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
x-unix-mode=0644;
name="wordfreq.java"
Content-Disposition: attachment;
filename=wordfreq.java
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class wordfreq {
static class Counter {
int count = 1;
}
public static void main(String[] args)
throws IOException
{
HashMap map = new HashMap();
Pattern charsOnly = Pattern.compile("\\p{Lower}+");
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = r.readLine()) != null) {
Matcher matcher = charsOnly.matcher(line.toLowerCase());
while (matcher.find()) {
String token = matcher.group();
Counter c = (Counter)map.get(token);
if (c != null)
c.count++;
else
map.put(token, new Counter());
}
}
ArrayList list = new ArrayList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
int c = ((Counter)((Map.Entry)o2).getValue()).count - ((Counter)((Map.Entry)o1).getValue()).count;
if (c == 0) {
c = ((String)((Map.Entry)o2).getKey()).compareTo((String)((Map.Entry)o1).getKey());
}
return c;
}
});
String[] padding = { "error!", " ", " ", " ", " ", " ", " ", "error!" };
StringBuffer output = new StringBuffer();
Iterator it = list.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String word = (String)entry.getKey();
String count = String.valueOf(((Counter)entry.getValue()).count);
if (count.length() < 7)
System.out.println(padding[7 - count.length()] + count + " " +word);
else
System.out.println(count + " " +word);
}
}
}
--Apple-Mail-15-426117134
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed
--Apple-Mail-15-426117134
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
x-unix-mode=0644;
name="message.java"
Content-Disposition: attachment;
filename=message.java
import java.util.*;
public class message {
public static final int numberOfThreads = 3000;
public static int numberOfMessagesToSend;
public static void main(String args[]) {
numberOfMessagesToSend = Integer.parseInt(args[0]);
MessageThread chain = null;
for (int i=0; i<numberOfThreads; i++){
chain = new MessageThread(chain);
new Thread(chain).start();
}
for (int i=0; i<numberOfMessagesToSend; i++) chain.enqueue(new Integer(0));
}
}
class MessageThread implements Runnable {
MessageThread nextThread;
List list = new ArrayList(4);
MessageThread(MessageThread nextThread){
this.nextThread = nextThread;
}
public void run() {
if (nextThread != null)
while (true) nextThread.enqueue(dequeue());
else {
int sum = 0;
int finalSum = message.numberOfThreads * message.numberOfMessagesToSend;
while (sum < finalSum)
sum += dequeue().intValue();
System.out.println(sum);
System.exit(0);
}
}
public void enqueue(Integer message)
{
synchronized(list) {
list.add(new Integer(message.intValue() + 1));
if (list.size() == 1) {
list.notify();
}
}
}
public Integer dequeue()
{
synchronized(list) {
while(list.size() == 0) {
try { list.wait(); } catch (Exception e) {}
}
return (Integer)list.remove(0);
}
}
}
--Apple-Mail-15-426117134--