import java.io.*; public class Read { public static void main(String args[]) throws Exception { if (args.length != 1) { System.err.println("Usage: java Read "); System.exit(1); } Class c = Class.forName(args[0]); Test t = (Test)c.newInstance(); for (int i=0; i<5; i++) { t.test(); } t.close(); } } abstract class Test { protected InputStream pg_input; Test() throws IOException { pg_input = new BufferedInputStream(new FileInputStream("/dev/zero"), 8192); } void close() throws IOException { pg_input.close(); } void test() throws IOException { long start = System.currentTimeMillis(); for (int i=0; i<100000000; i++) { ReceiveIntegerR(4); } long stop = System.currentTimeMillis(); System.out.println(stop - start); } protected abstract int ReceiveIntegerR(int siz) throws IOException; } class Orig extends Test { Orig() throws IOException { } protected int ReceiveIntegerR(int siz) throws IOException { int n = 0; for (int i = 0 ; i < siz ; i++) { int b = pg_input.read() & 0xFF; n = b | (n << 8); } switch (siz) { case 1: return (int)((byte)n); case 2: return (int)((short)n); default: return n; } } } class Two extends Test { Two() throws IOException { } protected int ReceiveIntegerR(int siz) throws IOException { int n = pg_input.read() & 0xFF; n = pg_input.read() | (n << 8); n = pg_input.read() | (n << 8); n = pg_input.read() | (n << 8); return n; } } class Three extends Test { private byte[] intbuf; Three() throws IOException { intbuf = new byte[4]; } protected int ReceiveIntegerR(int siz) throws IOException { pg_input.read(intbuf); return (intbuf[0] & 0xFF) << 24 | (intbuf[1] & 0xFF) << 16 | (intbuf[2] & 0xFF) << 8 | intbuf[3] & 0xFF; } }