aspose file tools*
The moose likes Performance and the fly likes what is better with respect to performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "what is better with respect to performance" Watch "what is better with respect to performance" New topic
Author

what is better with respect to performance

Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi,

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.

Regards,

Nuwan Arambage


Thinker
Nuwan Arambage
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

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'.

Hope this helps.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

@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.
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
now I got the idea.It is better to stick with object creation with new keyword except where you have a special requirement

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is better with respect to performance