• 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

Which of the two methods will execute faster

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which of the two methods will execute faster ?
[ August 13, 2005: Message edited by: Lavjeet Khanuja ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our Performance forum...
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really want to know why not time them?

long start = System.currentTimeMillis();
call the method
long duration = System.currentTimeMillis() - start;
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic