import java.io.*;
class StaticVsInstanceBenchmark
{
{
StaticVsInstanceBenchmark program = new StaticVsInstanceBenchmark();
program.run();
}
static final int DURATION = 1000;
{
doBenchmark( new VirtualTest( new ClassWithVirtualMethod() ),
new NonVirtualTest( new ClassWithNonVirtualMethod() ),
new StaticTest() );
}
void doBenchmark
( Test...
tests ) throws Exception {
System.
out.
println( " Name | Iterations" ); doBenchmark2( devNull, 1, tests ); //warmup
doBenchmark2
( System.
out, DURATION, tests
); System.
out.
println( "Done." ); }
{
for( Test test : tests )
{
long iterations = runTest( duration, test );
printStream.printf( "%15s | %10d\n", test.getClass().getSimpleName(), iterations );
}
}
long runTest
( int duration, Test test
) throws Exception {
test.terminate = false;
test.count = 0;
thread.start();
test.terminate = true;
thread.join();
return test.count;
}
static abstract class Test
implements Runnable {
volatile boolean terminate = false;
long count = 0;
}
static class ClassWithStaticStuff
{
static int staticDummy;
static void staticMethod() { staticDummy++; }
}
static class StaticTest extends Test
{
@Override
public void run()
{
for( count = 0; !terminate; count++ )
{
ClassWithStaticStuff.staticMethod();
}
}
}
static class ClassWithVirtualMethod
implements Runnable {
int instanceDummy;
@Override public void run() { instanceDummy++; }
}
static class VirtualTest extends Test
{
{
this.runnable = runnable;
}
@Override
public void run()
{
for( count = 0; !terminate; count++ )
{
runnable.run();
}
}
}
static class ClassWithNonVirtualMethod
{
int instanceDummy;
final void nonVirtualMethod() { instanceDummy++; }
}
static class NonVirtualTest extends Test
{
final ClassWithNonVirtualMethod objectWithNonVirtualMethod;
NonVirtualTest( ClassWithNonVirtualMethod objectWithNonVirtualMethod )
{
this.objectWithNonVirtualMethod = objectWithNonVirtualMethod;
}
@Override
public void run()
{
for( count = 0; !terminate; count++ )
{
objectWithNonVirtualMethod.nonVirtualMethod();
}
}
}
{
public void write(int b) {}
} );
}
aW1wb3J0IGphdmEuaW8uKjsKCmNsYXNzIFN0YXRpY1ZzSW5zdGFuY2VCZW5jaG1hcmsKewogICAgcHVibGljIHN0YXRpYyB2b2lkIG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1bigpCiAgICAgICAgewogICAgICAgICAgICBmb3IoIGNvdW50ID0gMDsgICF0ZXJtaW5hdGU7ICBjb3VudCsrICkKICAgICAgICAgICAgewogICAgICAgICAgICAJQ2xhc3NXaXRoU3RhdGljU3R1ZmYuc3RhdGljTWV0aG9kKCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgc3RhdGljIGNsYXNzIENsYXNzV2l0aFZpcnR1YWxNZXRob2QgaW1wbGVtZW50cyBSdW5uYWJsZQogICAgewoJCWludCBpbnN0YW5jZUR1bW15OwogICAgICAgIEBPdmVycmlkZSBwdWJsaWMgdm9pZCBydW4oKSB7IGluc3RhbmNlRHVtbXkrKzsgfQogICAgfQoKICAgIHN0YXRpYyBjbGFzcyBWaXJ0dWFsVGVzdCBleHRlbmRzIFRlc3QKICAgIHsKICAgIAlmaW5hbCBSdW5uYWJsZSBydW5uYWJsZTsKICAgIAkKICAgIAlWaXJ0dWFsVGVzdCggUnVubmFibGUgcnVubmFibGUgKQogICAgCXsKICAgIAkJdGhpcy5ydW5uYWJsZSA9IHJ1bm5hYmxlOwogICAgCX0KCiAgICAgICAgQE92ZXJyaWRlCiAgICAgICAgcHVibGljIHZvaWQgcnVuKCkKICAgICAgICB7CiAgICAgICAgICAgIGZvciggY291bnQgPSAwOyAgIXRlcm1pbmF0ZTsgIGNvdW50KysgKQogICAgICAgICAgICB7CiAgICAgICAgICAgIAlydW5uYWJsZS5ydW4oKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBzdGF0aWMgY2xhc3MgQ2xhc3NXaXRoTm9uVmlydHVhbE1ldGhvZAogICAgewoJCWludCBpbnN0YW5jZUR1bW15OwogICAgICAgIGZpbmFsIHZvaWQgbm9uVmlydHVhbE1ldGhvZCgpIHsgaW5zdGFuY2VEdW1teSsrOyB9CiAgICB9CgogICAgc3RhdGljIGNsYXNzIE5vblZpcnR1YWxUZXN0IGV4dGVuZHMgVGVzdAogICAgewogICAgCWZpbmFsIENsYXNzV2l0aE5vblZpcnR1YWxNZXRob2Qgb2JqZWN0V2l0aE5vblZpcnR1YWxNZXRob2Q7CiAgICAJCiAgICAJTm9uVmlydHVhbFRlc3QoIENsYXNzV2l0aE5vblZpcnR1YWxNZXRob2Qgb2JqZWN0V2l0aE5vblZpcnR1YWxNZXRob2QgKQogICAgCXsKICAgIAkJdGhpcy5vYmplY3RXaXRoTm9uVmlydHVhbE1ldGhvZCA9IG9iamVjdFdpdGhOb25WaXJ0dWFsTWV0aG9kOwogICAgCX0KCiAgICAgICAgQE92ZXJyaWRlCiAgICAgICAgcHVibGljIHZvaWQgcnVuKCkKICAgICAgICB7CiAgICAgICAgICAgIGZvciggY291bnQgPSAwOyAgIXRlcm1pbmF0ZTsgIGNvdW50KysgKQogICAgICAgICAgICB7CiAgICAgICAgICAgIAlvYmplY3RXaXRoTm9uVmlydHVhbE1ldGhvZC5ub25WaXJ0dWFsTWV0aG9kKCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgc3RhdGljIGZpbmFsIFByaW50U3RyZWFtIGRldk51bGwgPSBuZXcgUHJpbnRTdHJlYW0oIG5ldyBPdXRwdXRTdHJlYW0oKSAKICAgIHsKICAgICAgICBwdWJsaWMgdm9pZCB3cml0ZShpbnQgYikge30KICAgIH0gKTsKfQo=