Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #8861
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Getter performance |
| Date | 2011-10-16 09:47 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <j7en9t$r5o$1@dont-email.me> (permalink) |
| References | <4e99f537$0$623$426a74cc@news.free.fr> |
On 10/15/2011 5:03 PM, Aéris wrote:
>
> I work on an application where performances are important.
>
> To optimize it, I thought a direct access to a variable (foo.bar) would
> be more efficient than a getter call (foo.getBar()).
> I thought by avoiding the call to a method and all that goes with it
> (context switching, stacking, return value…), I can save time, but this
> code sample prove the contrary :
> http://pastebin.com/bP1nqxce
> Direct variable access : 1041 ms
> Getter call : 556 ms
>
> The difference is even more important if I don't modify the variable
> value (lines 29 and 36 commented) :
> Direct variable access : 95 ms
> Getter call : 4 ms
>
> How can we explain this not obvious huge difference ( 50 and 95% ) ?
Insufficiently precise measurement. You are trying to tell
whether two nickels are heavier than four pennies by piling each
stack of coins in turn atop a bulldozer and weighing the bulldozer.
If you want to measure tiny effects, seek a measurement framework
that doesn't involve huge effects.
Fundamentally, though, you're going about the larger task in
the wrong way: You are simply guessing that the speed of access to
a variable is important to the performance of your application. Do
you have even the slightest shred of evidence that this is so? Or
are you trying to improve your car's fuel economy by scraping off
all the paint to reduce the vehicle weight?
1: Decide what performance criteria the application must satisfy.
Make this quantifiable: "Throughput of at least X doodads per eyeblink"
or "95% of responses in no more than Y milliquivers," not undecidable
like "As fast as possible."
2: (Optional) Make back-of-the-envelope calculations about data
rates, data set sizes, and so on, just to estimate the resources
that will be required. Is the load within reach of a laptop, or do
you need a clustered network of supercomputers? Can you handle the
traffic with WiFi, or do you need sixteen 10Gb/s fibers in parallel?
3: Write the application, as cleanly and simply as you can in
light of [1] and [2]. During this stage, bear in mind that even the
most expert of programmers is usually surprised about where his code
spends the majority of its time (this is not opinion; it's been found
over and over again in actual experiment). In other words, favor
"clean and simple" over "clever and reputedly fast."
4: Measure the performance, and compare to the thresholds of [1].
If you satisfy [1], stop!
5: (Only if [1] isn't satisfied) Apply profilers, debuggers, and
other tools to discover which parts of the program are too slow. Apply
your efforts to speeding up those parts only; pay no attention to the
others. Then return to [4]. In very rare cases (usually when [2] was
skipped or when some external agency forces a change in [1]), return
to [3] and start over.
Do not begin by trying to micro-optimize; that way lies madness.
Trust me; I know: Been there, done that, bear the scars.
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Getter performance Aéris <aeris@imirhil.fr> - 2011-10-15 23:03 +0200
Re: Getter performance Arne Vajhøj <arne@vajhoej.dk> - 2011-10-15 17:36 -0400
Re: Getter performance Arne Vajhøj <arne@vajhoej.dk> - 2011-10-15 17:42 -0400
Re: Getter performance BGB <cr88192@hotmail.com> - 2011-10-15 15:00 -0700
Re: Getter performance markspace <-@.> - 2011-10-15 15:20 -0700
Re: Getter performance David Lamb <dalamb@cs.queensu.ca> - 2011-10-20 12:45 -0400
Re: Getter performance Roedy Green <see_website@mindprod.com.invalid> - 2011-10-21 14:27 -0700
Re: Getter performance Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-21 18:57 -0700
Re: Getter performance Patricia Shanahan <pats@acm.org> - 2011-10-22 07:27 +0100
Re: Getter performance Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-22 09:57 -0300
Re: Getter performance Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-21 22:12 -0400
Re: Getter performance Aéris <aeris@imirhil.fr> - 2011-10-15 23:59 +0200
Re: Getter performance Arne Vajhøj <arne@vajhoej.dk> - 2011-10-15 19:44 -0400
Re: Getter performance Aéris <aeris@imirhil.fr> - 2011-10-16 13:14 +0200
Re: Getter performance Lars Enderin <lars.enderin@telia.com> - 2011-10-16 16:28 +0200
Re: Getter performance Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-16 09:47 -0400
Re: Getter performance Jaap Droogers <JaapDroogers@unusable.meel.homelinux.net> - 2011-10-16 22:12 +0200
Re: Getter performance BGB <cr88192@hotmail.com> - 2011-10-16 13:58 -0700
Re: Getter performance David Lamb <dalamb@cs.queensu.ca> - 2011-10-20 12:51 -0400
Re: Getter performance Paul Cager <paul.cager@googlemail.com> - 2011-10-21 08:49 -0700
Re: Getter performance Roedy Green <see_website@mindprod.com.invalid> - 2011-10-21 08:02 -0700
Re: Getter performance Wanja Gayk <brixomatic@yahoo.com> - 2011-10-22 21:11 +0200
csiph-web