• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

creating Objects vs using a reference

 
Ranch Hand
Posts: 128
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

An object is created in memory. Therefore, creating 1000's of them is slower than creating one in a constructor and using its reference. What does everybody think?

I'm fine tuning an app for performance. Yes, micro tuning is not always recommended, but this is one of my last resorts.

Thanks
 
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you reuse Objects across threads, you have to take consider thread safety issues.

Creating 1000 objects will use up more memory than creating 1 object. The speed depends on the time needed to run the code in the constructor.

Good luck.
 
Allen Bandela
Ranch Hand
Posts: 128
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if there's no code in the constructor. object's take memory , correct, but irrespective of their constructor code, wouldn't creating something in memory take x time.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Optimizing programs is not so easy. Far too often programmers just look at the source and modify things that they think are slow - not things that they know are slow.

The right way to optimize a program for speed (or memory usage) is to use a profiler to measure the performance of the program and to find out where the bottleneck is. Then you concentrate on improving the bottleneck. Micro-optimizing a program without knowing where the actual bottleneck in the program is, is a waste of time.

So, did you use a profiler - do you actually know where the bottleneck is in your program? Or are you doing the optimization based on theoretical ideas?
 
Allen Bandela
Ranch Hand
Posts: 128
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did use a profiler....actually they're logging statements that calculate the time for each method invocation. I see that some methods take time....but then there's no algorithm that needs improvement. Its more like the requirements now want the program to run faster because of a change in user input. And I'm trying to solve that without hindering correctness.
[ April 13, 2007: Message edited by: Sylvester James ]
 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sylvester James:
what if there's no code in the constructor. object's take memory , correct, but irrespective of their constructor code, wouldn't creating something in memory take x time.



You can try creating lots of Objects and see how long it takes. I believe the time required to create lots of objects is very minute (independent of how large/complex the class is), unless there are heavyweight operations within the constructor.

A poor but reasonable example: Creating a SimpleDateFormat requires a pattern String. It is imagineable that some processing took place when you instantiate the SimpleDateFormat with a pattern, such that subsequent calls to format dates can be optimized.

If you are creating SimpleDateFormat with the same pattern String over and over again, it may be a good idea to reuse the same object and save some processing time.
reply
    Bookmark Topic Watch Topic
  • New Topic