On Fri, May 3, 2013 at 4:00 PM, Sasha Pachev <[email protected]> wrote:

> Bryan:
>
> bash-4.1$ java -version
> java version "1.6.0_27"
> Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
> Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)
>

OpenJDK 7 should be noticeably faster.  Also, be sure to use the "-server"
option -- that uses a more aggressive JIT compiler.

# yum install java-1.7.0-openjdk
(or similar for your distro)

- however, this is a disaster:
>
> 24410 write(1, "0801", 4)               = 4
> 24410 write(1, "\n", 1)                 = 1
> 24410 write(1, "tniop-01", 8)           = 8
> 24410 write(1, "\n", 1)                 = 1
> 24410 write(1, "ht01", 4)               = 4
>
> and that is where most of 0.8s of system time is from, I think. If you
> buffered your output, you may see much better results.
>

This is a problem, so I buffered the output.  I get the following times on
my machine:

words:
real 0m0.208s
user 0m0.293s
sys 0m0.030s

words6:
real 0m0.423s
user 0m0.644s
sys 0m0.082s

words12:
real 0m0.632s
user 0m0.914s
sys 0m0.115s

words24:
real 0m1.057s
user 0m1.331s
sys 0m0.204s

words48:
real 0m1.869s
user 0m2.011s
sys 0m0.372s

Now that the excessive system calls have been eliminated, I think we can
see the JIT doing its magic.  Java code below:

package org.plug;

import java.io.BufferedReader;
import java.io.FileReader;

public class LineReverse {
    public static void main(String... args) throws Exception {
        StringBuilder sb = new StringBuilder(1000);
        String filename = (args.length > 0) ? args[0] :
"/usr/share/dict/words";
        BufferedReader in = new BufferedReader(new FileReader(filename));
        StringBuilder out = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) {
            sb.setLength(0);
            sb.append(line);
            sb.reverse();
            out.append(sb);
            out.append('\n');
        }
        System.out.print(out);
    }
}

-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