This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Performance and the fly likes Improve performance Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Performance
Reply 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: 4901
    
    7

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?
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: 10040
    
    6

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


Never ascribe to malice that which can be adequately explained by stupidity.
Ivan Jozsef Balazs
Ranch Hand

Joined: May 22, 2012
Posts: 449
    
    1
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: 4901
    
    7

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Improve performance
 
Similar Threads
How many objects eligible for GC??
Garbage collection program really confusing me?
Generic Problem
Pass by reference problem in EJB's
Question about Sequence Diagram