This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

what is better with respect to performance

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now I got the idea.It is better to stick with object creation with new keyword except where you have a special requirement

 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic