Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #8871
| From | BGB <cr88192@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Getter performance |
| Date | 2011-10-16 13:58 -0700 |
| Organization | albasani.net |
| Message-ID | <j7fghg$3va$1@news.albasani.net> (permalink) |
| References | <4e99f537$0$623$426a74cc@news.free.fr> <4e9b3abe$0$31993$e4fe514c@dreader24.news.xs4all.nl> |
On 10/16/2011 1:12 PM, Jaap Droogers wrote: > On 15-10-11 23:03, schreef Aéris: >> Hello everybody. >> >> 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% ) ? >> > > You forgot the JIT compiler. All the nice clean code you have written is > optimized by the compiler. You don't write C code, remember. note: many C and C++ compilers will perform the same optimization in many cases. some compilers will require both to be present in the same compilation unit, but some others (such as GCC and LLVM/Clang) can make use of link-time optimization. these sorts of optimizations are nothing specific to the JVM. > Why is Java so fast: it uses the JIT compiler which optimizes the code > for the machine the program is running on. > if you write > int a = myObject.getA(); > > the compiler compiles it probably as: > int a = myObject.a; > > There is also a change that you try to optimize your code and the JIT > compiler does not now how to optimize it, so your code runs slower. > yep. at the CPU level the reference may potentially compile down to a single "mov" instruction. if the getter is called but its value isn't used, then there is a chance that it may be discarded altogether (so no CPU instructions are produced in this case). if trying to micro-benchmark or micro-optimize things (yes... including in C), one will usually spot this case by the running time dropping to a very small number (IOW: the code runs impossibly fast). generally though, it is not really productive to worry about micro-optimizing things. better IMO to leave optimizing for when and where it actually matters.
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