Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: BGB Newsgroups: comp.lang.java.programmer Subject: Re: Getter performance Date: Sun, 16 Oct 2011 13:58:16 -0700 Organization: albasani.net Lines: 65 Message-ID: References: <4e99f537$0$623$426a74cc@news.free.fr> <4e9b3abe$0$31993$e4fe514c@dreader24.news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net AWELEDNHe+vlZ9MW/o/4FAb3DizGVJjjphxCUiHbE2YxbmsUiumn4FCCAcjWtr5il4G/cCJxV4F8Xeu0aRcICg== NNTP-Posting-Date: Sun, 16 Oct 2011 20:58:24 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="QG8lTOlSrOc8ThtBdYeWq1/jEl2px5L3QY9QhsZbkox1cQpuiWHxloZ+D3dFCJxif/vt1nEh0b7OrHlpjv9o7v/NQkg+NoyFqCZgo6/4CPtVJioRTJWzKOLZmwh+Y8pM"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 In-Reply-To: <4e9b3abe$0$31993$e4fe514c@dreader24.news.xs4all.nl> Cancel-Lock: sha1:33G1G6/VpVFM+aJxRL89iukInsI= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8871 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.