It's time to wake up and really face a challenge. This is short, but it should take some work. This challenge is to find a small, super fast algorithm that counts the number of non-space characters in any given String. To play, just do something like this:
public class Main {
public static void main(String[] args) {
String s = "This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string" +
"This is a really long string";
// Output the result.
System.out.println("Chars outputted: " + countNumChars(s));
// Now get average of 1000 trials.
long before = System.nanoTime();
for (int i = 0; i < 1000; i++) {
countNumChars(s);
}
long after = System.nanoTime();
System.out.println("Time (in nanoseconds): " + ((after-before))/1000.0);
}
private static int countNumChars(String s) {
}
}
Just fill in the body for the countNumChars method or change it a little bit and see if you can beat everybody else. The fastest algorithm wins!
Please post your code and the results in your post.
*For the benefit of those functional coders who use a JVM functional language, just create something that mimics this output so that we can properly test it.
Get coding! The deadline is January 5th, which gives you all a week to find the fastest algorithm.
This post has been edited by Dogstopper: 22 December 2010 - 10:18 AM

New Topic/Question
Reply






MultiQuote





|