I need to heavily optimize a piece of java code to the point that I'm wondering if I should use local variables in methods, singletons with private variables or "static classes with all static variables.
NOTE: I never read these variables, I initialize them anew on each method call, so there is no fetching from any form of memory.
So I Made some tests, given my situation and the result baffled me:
public class Test {
private int singleton;
private static int staticc;
private static Test te;
public static void main(String[] args) {
te = new Test();
for (int i = 0; i < 10; i++)
t();
}
private static void t(){
long now = System.nanoTime();
for (int i = 0; i < 1; i++){
for (int j = 0; j < 1000000000; j++){
te.singleton();
}
}
System.out.println("singleton: " + (System.nanoTime() - now));
now = System.nanoTime();
for (int i = 0; i < 1; i++){
for (int j = 0; j < 1000000000; j++){
staticc();
}
}
System.out.println("static: " + (System.nanoTime() - now));
now = System.nanoTime();
for (int i = 0; i < 1; i++){
for (int j = 0; j < 1000000000; j++){
local();
}
}
System.out.println("local: " + (System.nanoTime() - now));
now = System.nanoTime();
for (int i = 0; i < 1; i++){
for (int j = 0; j < 1000000000; j++){
overhead();
}
}
System.out.println("overhead: " + (System.nanoTime() - now));
}
private void singleton(){
singleton = 1;
singleton += singleton;
}
private static void staticc(){
staticc = 1;
staticc += staticc;
}
private static void local(){
float local = 1;
local += local;
}
private static void overhead(){
return;
}
}
Which gave me the result:
singleton: 24460647
static: 41413757
local: 2630802
overhead: 2488400
singleton: 30776796
static: 19671150
local: 5132
overhead: 5131
singleton: 19669867
static: 144606898
local: 5559
overhead: 5560
singleton: 19399175
static: 143904724
local: 855
overhead: 428
singleton: 18892001
static: 143648143
local: 427
overhead: 428
singleton: 19543287
static: 145828647
local: 855
overhead: 428
singleton: 19356838
static: 145032821
local: 855
overhead: 1711
singleton: 19559965
static: 143845710
local: 427
overhead: 428
singleton: 19744275
static: 145983878
local: 428
overhead: 855
singleton: 19622399
static: 145635357
local: 428
overhead: 427
Local variables are fastest, no surprise here. The local part of the program is also optimized at some stage (since it doesn't really do anything), so that it is skipped entirely. What baffles me is that singleton is up to 10x faster than static access.
My question is: could it be that singletons are indeed faster that static classes, or is it just my circumstances that makes the test misleading?
Regarding Singleton: I changed to code. Added this class:
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private int sing = 0;
public static Singleton getInstance(){
return INSTANCE;
}
public void single(){
sing = 1;
sing += sing;
}
}
Which is exactly what I had nested into the original class, but just did it for clarification. It is a singleton, is it not?
Then I changed this code in the main class:
long now = System.nanoTime();
for (int i = 0; i < 1; i++){
for (int j = 0; j < 1000000000; j++){
Singleton.getInstance().single();
}
}
System.out.println("singleton: " + (System.nanoTime() - now));
I got the same results.