when I read a article about prototype pattern, it is said that creating object using new keyword is expensive task.when it comes to performance consideration what is better.
I mean, can somebody give the benchmark of the both techniques.
I have a another question why object creation using new keyword is slow.
The article you read may have been based on outdated information. Object creation used to be slow in the early day of Java, but has become quite a bit faster over time, especially with the JVMs in Java 5 and Java 6. Unless you have specific evidence that object creation is indeed a performance bottleneck in your application, you should assume that it is not.
That's also the reason why object pools (like thread pools) are no longer a hot topic - they're not generally worth the effort any more.
Also, it should be noted that the comparison you want to make is between 'upfront creation of a pool of objects and on-demand use of them' as opposed to 'on-demand creation of objects'. The comparison is NOT between using 'new' to create an object vs. some other way of creation (whatever that might be).
The upfront creation of objects in pool is also (mostly) done via 'new', I believe. The other ways include reflection and deserialization, which cannot be faster than 'new' anyway.
So, it all boils down to - Is the creation of the object in question too expensive (constructor takes too long), so much so, that it can't be used on-demand?
If yes, pool the objects. However, be careful about reusing methodology if they are stateful.
If not, stick with KISS principle, and use on-demand 'new'.
@Lester I hope when you say that Object Pools are not worth the effort anymore, you are referring to custom creation of thread-pools and connection-pools etc. I agree, this should not be done except either in very very extreme cases, or academic purposes.
However, as for using these (and other) pools, it is highly recommended that one uses a pool of pre-constructed objects in situations where object-creation is heavy in terms of native resources. For example, a database connection.
Joined: May 05, 2010
now I got the idea.It is better to stick with object creation with new keyword except where you have a special requirement
subject: what is better with respect to performance