I'm having some trouble with a game developed in Java which is kinda slow. I benchmarked it a little bit and found the problem: most of the time, the JVM is in the Object.<init>()
since there are a lot of objects which gets created and destroyed every tick.
This class which gets created this often is a really small class, just 3 integers, nothing more. But this was a problem I tried to solve. So I started a few tests on how I could increase the speed. What I found is unexpected. First off, code I tested with is as follwos:
public class NewVsCloneTest implements Cloneable {
int a = 0;
int b = 0;
int c = 0;
public static void main(String... args) throws Exception {
Stack h = new Stack();
System.out.println("Benchmarking Copy vs Clone...");
long time = 0;
long n = 0, c = 0, s = 0;
NewVsCloneTest obj = null;
for(int num = 0; num < 50; num++){
time = System.currentTimeMillis();
for(int i = 0; i < 10000000; i++){
obj = new NewVsCloneTest();
}
n += (System.currentTimeMillis() - time);
System.out.println("New took " + (System.currentTimeMillis() - time) + " MilliSeconds");
time = System.currentTimeMillis();
for(int i = 0; i < 10000000; i++){
obj = (NewVsCloneTest) obj.clone();
}
c += (System.currentTimeMillis() - time);
System.out.println("Clo took " + (System.currentTimeMillis() - time) + " MilliSeconds");
time = System.currentTimeMillis();
for(int i = 0; i < 10000000; i++){
obj = h.getOne();
h.pushOne(obj);
}
s += (System.currentTimeMillis() - time);
System.out.println("Sta took " + (System.currentTimeMillis() - time) + " MilliSeconds");
}
System.out.println("After 50 runs, new took " + n + ", clone took " + c + " and stack took " + s);
System.out.println("On average, new took " + n / 50.0 + ", clone took " + c / 50.0 + " and stack took " + s / 50.0 + " per 10 mio runs.");
}
public static class Stack {
private NewVsCloneTest[] vars = new NewVsCloneTest[10];
{
for(int i = 0; i < 10; i++)
vars[i] = new NewVsCloneTest();
}
int pos = 9;
public NewVsCloneTest getOne(){
return vars[pos--];
}
public void pushOne(NewVsCloneTest var){
vars[++pos] = var;
}
}
}
The results I get:
After 50 runs, new took 3462, clone took 5784 and stack took 4132
On average, new took 69.24, clone took 115.68 and stack took 82.64 per 10 mio runs.
So, this was really unexpected for me, that new is faster than the small stack-implementation. I mean, the stack already can do nothing but get and put one. No rangechecks, no size-change, just nothing. I even gave "NewVsCloneTests" a few more ints, a few longs, a few objects (just nullptr's) and a few chars, but got the same outcome.
So the thing is, I've seen a lot of source code where one cached instances of classes which will get needed very often but just for a short time, so I thought it would make a difference. But my results show me that even worse.
How can I handle this particular problem?
The class which gets instantiated this often is just a dataholder for coordinates (x, y, z), and it gets instantiated this often (copy-constructed if you will) because I can't hand out my originals.
NewVsCloneTest
can be initialized with statically-known data, and that data happens to be the default value for all the fields. That probably makes a difference. The JVM might also be optimizing the instantiations away since you don't actually do anything with them, while theStack
code has to decrement and increment a variable and the array code has to check the index and that the class of object can be stored in that kind of array. Try adding an operation like incrementinga
,b
orc
and see if the relative speeds.