Re: Minor performance improvements
On Tue, 27 Feb 2007, Stephen Denne wrote:
I'm sad to say that I have not created any micro-benchmark tests, and
unfortunately the improvements are very minor, and far overshadowed by
the variability I get from my system.
I've created the attached test which tests the original code (Orig), your
code (Two), and my suggestion of an int4buf (Three) and got the following
surprising results:
jurka(at)tony:~/pg/jdbc/projects/perf/micro$ java -classpath . Tester Orig |
sort -n
11335
11370
11468
11484
11487
jurka(at)tony:~/pg/jdbc/projects/perf/micro$ java -classpath . Tester Two |
sort -n
12472
12476
12489
12492
12619
jurka(at)tony:~/pg/jdbc/projects/perf/micro$ java -classpath . Tester Three |
sort -n
4259
4562
4564
4611
4689
This shows your code is actually slower than the original code, although I
have no idea why that could be. It shows the int4buf idea as a clear
winner. I'm a little suspicious of the whole test because of your numbers
going up. Could you take a look at this and possibly confirm the results?
I'm not sure if windows has an equivalent to /dev/null, but I wanted to
avoid any impact of disk io.
Kris Jurka
import java.io.*;
public class Tester {
public static void main(String args[]) throws Exception {
if (args.length != 1) {
System.err.println("Usage: java Tester <iml>");
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 OutputStream pg_output;
Test() throws IOException {
pg_output = new BufferedOutputStream(new FileOutputStream("/dev/null"), 8192);
}
void close() throws IOException {
pg_output.close();
}
void test() throws IOException {
long start = System.currentTimeMillis();
for (int i=0; i<100000000; i++) {
SendInteger4(i);
}
long stop = System.currentTimeMillis();
System.out.println(stop - start);
}
protected abstract void SendInteger4(int val) throws IOException;
}
class Orig extends Test {
Orig() throws IOException { }
private void SendChar(int val) throws IOException
{
pg_output.write((byte)val);
}
protected void SendInteger4(int val) throws IOException
{
SendChar((val >> 24)&255);
SendChar((val >> 16)&255);
SendChar((val >> 8)&255);
SendChar(val&255);
}
}
class Two extends Test {
Two() throws IOException { }
protected void SendInteger4(int val) throws IOException
{
pg_output.write(val >> 24);
pg_output.write(val >> 16);
pg_output.write(val >> 8);
pg_output.write(val);
}
}
class Three extends Test {
private byte[] intbuf;
Three() throws IOException {
intbuf = new byte[4];
}
protected void SendInteger4(int val) throws IOException
{
intbuf[0] = (byte)((val >> 24) & 255);
intbuf[1] = (byte)((val >> 16) & 255);
intbuf[2] = (byte)((val >> 8) & 255);
intbuf[3] = (byte)((val) & 255);
pg_output.write(intbuf);
}
}
Home |
Main Index |
Thread Index