[Beowulf] C vs C++ challenge (java version)

Mike Eggleston mikee at mikee.ath.cx
Thu Jan 29 14:56:34 EST 2004


On Thu, 29 Jan 2004, dc wrote:

> Jakob Oestergaard wrote:
> 
> > Let me throw out a challenge:
> >
> > "Write a program in C that reads from standard input until EOF, and
> >  prints the number of distinct words found in that input"
> >
> <snip>
> >
> > I use an RB-tree to store the words I find, and print the number of
> > elements in the final tree.
> >
> > A comparable C solution shouldn't be more than a few thousand lines ;)
> >
> > Oh, right, I cheated and used STL.  Well, STL is a big part of why C++
> > is really handy, but go right ahead and use "glib" or whatever.
> >
> > I still guarantee you two things:
> > 1) Your code will be longer
> > 2) Your program will be slower
> >
> > As always, I love to be proven wrong  ;)
> 
> 
> Here is another try at that, this time in Java.
> 
> C++ code compiled with g++ 3.3 (-O3), times are 'real time' reported by the
> bash time function, the client and server columns correspond to java runs using
> the -client and -server options respectively.
> Java compiler and VM are the hotspot 1.4.
> 
> 
> file            size        C++         j client    j server
> wrnpc10.txt     3282452     0m2.746s    0m1.941s    0m1.643s
> shaks12.txt     5582655     0m4.476s    0m3.321s    0m2.842s
> big.txt         39389424    0m29.120s   0m13.972s   0m12.776s
> vbig.txt        196947120   2m23.882s   1m5.707s    1m2.350s
> 
> Oh right, I cheated and used Java standard collections ;-)
> 
> Seriously tough, Java is not as slow as some seem to believe. 
> 
> And optimizing the code at run-time (hotspot, code morphing,...) has the
> advantage that the optimizer has access to potentially useful information, both
> about the code itself and about the executing environment.
> 
> --------------------------------------------------------------
> import java.util.*;
> import java.util.regex.Pattern;
> import java.io.*;
> 
> 
> public class wordcount {
> 
>     public static void main (String args[]) {
>         HashSet hs = new HashSet();
>         try {
>             BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
>             
>             String s, words[];
>             Pattern p = Pattern.compile("\\s");
>             
>             while ((s = input.readLine()) != null) {
>                 words = p.split(s);
>                 for (int i = 0; i < words.length; i++)
>                     hs.add(words[i]);
>             }
>             hs.remove("");
>         }
>         catch (Throwable e) {
>             e.printStackTrace();
>         }
>         System.out.println("Words: " + hs.size());
>     }
> }

Do you have some time to run the same test using the gcc front-end
to java and posting those results?

Mike
_______________________________________________
Beowulf mailing list, Beowulf at beowulf.org
To change your subscription (digest mode or unsubscribe) visit http://www.beowulf.org/mailman/listinfo/beowulf



More information about the Beowulf mailing list