I'm currently trying to build some code execution optimization for a contest, and was looking at the ObjectPool pattern to favor object reuse instead of new object instantiation.
I've put together a small project (and the only test class) to investigate some of the things I see and don't understand.
What I'm doing:
The results I have are:
Figures are for new instantiation vs with object pool for 5 000 000 iterations
without_warmup_without_new_object_use_with_random_parameters: 417 vs 457
without_warmup_without_new_object_use_with_fixed_parameters: 11 vs 84
without_warmup_with_new_object_use_with_random_parameters: 515 vs 493
without_warmup_with_new_object_use_with_fixed_parameters: 64 vs 90
with_warmup_without_new_object_use_with_random_parameters: 284 vs 419
with_warmup_without_new_object_use_with_fixed_parameters: 8 vs 55
with_warmup_with_new_object_use_with_random_parameters: 410 vs 397
with_warmup_with_new_object_use_with_fixed_parameters: 69 vs 82
What I notice from that:
What I'm looking for here is to understand these results, and get pointers to docs / books that I could read to get a good knowledge of what happens behind the scenes in these cases.
Thanks!
As mentioned in the comment by Mike Nakis the difference between your tests with random parameters vs those with fixed parameters is entirely due to the expense of generating the random number, a fairer test might be to generate a 10 million entry array of random integers (1 for each parameter needed to initialise a Point
) before engaging the loop and comparing that to a 10 million entry array of number picked by you (i.e 1 and 2) that way you are comparing like for like, without including the expense of the random number generation in your test results.
The reason why your pool is performing worse than initialising new objects each time(at least in terms of execution time), is because the object that you are storing in your pool is a relatively trivial object that will take next to no time to initialise. As such the conditional statement that you are evaluating:
if (pointIndex >= POINT_POOL_SIZE) {
pointIndex = pointIndex - POINT_POOL_SIZE;
totalPointLoops++;
}
as well as indexing the array, is requiring more execution time than your Point
object needs for init.
You might be making some memory saving in your example, however this seems unlikely as the objects that you take from the pool are never released back to the pool for reuse(from what i can see in your code). Also a simple loop probably isnt the best way to test an object pool as the purpose of an object pool is to have a cache of objects that are expensive to create/prone to failure/etc, and they tend to get used for longer than a simple loop iteration, chances are you are only really using one object at a time.
Here are some good links to information on object pools in Java:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With