aspose file tools*
The moose likes Performance and the fly likes Which of the two methods will execute faster Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Which of the two methods will execute faster" Watch "Which of the two methods will execute faster" New topic
Author

Which of the two methods will execute faster

Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268


Which of the two methods will execute faster ?
[ August 13, 2005: Message edited by: Lavjeet Khanuja ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

1) What do you think?
2) Why does it matter?
3) Why aren't you using StringBuffers?

I'm not trying to be rude, just trying to work out the purpose of the question. I'd guess m1(), but if you can tell the difference in the timing of the two I'd be amazed
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

If the question is what I think it is -- "Is member variable access or local variable access faster?" -- then the answer is actually m2(), because local variable access is considerably faster. But as David correctly points out, this difference will be lost in the noise of allocating thousands of Strings in both of these methods. You won't be able to measure a difference.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to our Performance forum...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
If you really want to know why not time them?

long start = System.currentTimeMillis();
call the method
long duration = System.currentTimeMillis() - start;
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
I think that it depends on the mechanism running behind, the memory allocation, the status of memory fragmentation...etc. I think that many conditions will be included when we want to know which one is faster.


Francis Siu
SCJP, MCDBA
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
str is never initalized, so I would think it would give a null pointer exception.

If your question is which will be faster, references to a local or instance variable I suspect if the difference is minimal and purely academic.



http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
str is never initalized, so I would think it would give a null pointer exception

No, it will run just fine...

I am sure that the difference is purely academic, and it would be almost impossible to tell the difference... but in theory, m2() should be faster.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Just in case anyone is curious (or bored like I was), I ran a timing test calling each of these methods 1000 times. Here are the results (no big shock):

Average time for m1(): 33 ms
Highest time for m1(): 62 ms
Lowest time for m1(): 31 ms

Average time for m2(): 33 ms
Highest time for m2(): 47 ms
Lowest time for m2(): 31 ms
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Paul Bourdeaux:
Just in case anyone is curious (or bored like I was), I ran a timing test calling each of these methods 1000 times. Here are the results (no big shock):


Two important suggestions for java microbenchmarks in general:

- have you tried to increase the number of iterations?
- have you tried to switch the order of execution?

Both things are important to notice the effect of the ongoing optimization of the hotspot engine...
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
I would guess many more executions than 1000 would be needed to show a performance difference. On my rather old pc I can execute 60,000,000 method executions in 1 second (a noop method call), and referencing a variable should be much faster than that! That is why we say the question is purely academic.

Being as referencing instance varable/local variables would both be so fast it would be hard to accurately measure a performance difference with the much more expensive string concatenation in the loop. For example the garbage collector or some other process on the computer may fire during one or the other of the routines and make the comparisons meaningless. I would avoid all the object allocation that is done in this example.

Also, even if you measure a performance difference I suspect it may be different in another JVM, or on another platform. So your new found knowledge may be very transient.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which of the two methods will execute faster
 
Similar Threads
reading character from a string array
array confusion
converting string array to string
Linux and Java Threads
stuck on null pointer