File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Improve performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Improve performance" Watch "Improve performance" New topic
Author

Improve performance

Paras Ahuja
Ranch Hand

Joined: May 22, 2012
Posts: 62
Hi everyone,
I would like to ask a general question:
Which one is faster: to create a new object every time or to store an object in a file(using serialization) and retrieve it whenever needed.
Alexei Kaigorodov
Greenhorn

Joined: Feb 24, 2008
Posts: 17
Unless it is required to make heavy calculations to fill object's content, creation is faster. Note that retrieving from database also includes object creation, and access to database itself requires a lot of processor's cycles.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Alexei Kaigorodov wrote:Note that retrieving from database also includes object creation, and access to database itself requires a lot of processor's cycles.

Although I agree with you in general, it's worth noting that the whole reason that databases exist is to persist and retrieve data; so, while for an individual object what you say is right, if Paras needs to store lots of objects, a database (NOT a file) is probably the way to go.

@Paras: Either way, serialization, while convenient, is extremely slow. It's more often used with Streams to pass entire objects across a network than as a primary persistence mechanism. And, unless you're dealing with very small amounts of data, a database is probably better than using files - in fact, with a 'wrapped' db like JavaDB it may even be better for individual objects too.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Paras Ahuja
Ranch Hand

Joined: May 22, 2012
Posts: 62
Well, i only know how to store objects in a file. Let me know how to do the same in database.
Also, another performance related question:
Is it better to import whole packages or just the classes we need to use.
I am making quite a big project and such minute things are affecting my performance (i think)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10923
    
  12

Paras Ahuja wrote:Is it better to import whole packages or just the classes we need to use.
I am making quite a big project and such minute things are affecting my performance (i think)

it's not. All an import statement does is tell the compiler "Hey, when I say 'class', i really mean 'fully.qualified.name.of.class".

So having a lot of imports may slow down your compile phase, it will have ZERO impact on your run-time performance.

(at least, this is what I have read many, many times).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 865
    
    5
fred rosenberger wrote:
So having a lot of imports may slow down your compile phase, it will have ZERO impact on your run-time performance.
(at least, this is what I have read many, many times).


You can prepare a test case: two versions of a class, one with fully qualified class names, the other the same but with imports,
and then you can disassemble the resulting classes to verify they are essentially identical.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Paras Ahuja wrote:Well, i only know how to store objects in a file.

Then life will be SLOW.

Let me know how to do the same in database.

No. It would take far too long. Try the tutorials or (possibly better) get a good book on the subject.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Improve performance
 
Similar Threads
Pass by reference problem in EJB's
Question about Sequence Diagram
How many objects eligible for GC??
Generic Problem
Garbage collection program really confusing me?