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: Sat, 15 Oct 2011 15:00:44 -0700 Organization: albasani.net Lines: 43 Message-ID: References: <4e99f537$0$623$426a74cc@news.free.fr> <4e99fcdf$0$295$14726298@news.sunsite.dk> <4e99fe53$0$281$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net s2SOl3KoS3AhukBV2vz5/9G85KeCoVaiPqpVUCXKCElXbaixD4OeqySxStC6tezbxefBdoPFQfSL/F6gRwedRQ== NNTP-Posting-Date: Sat, 15 Oct 2011 22:00:57 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="P6ZfDGI8eKMKo+gunL+BZWHLYTnP/tCZiC5E3VSRofVUux0KwS2cKWAsEjQHll7H5qPRHRpmbC9q/E09R8OC1XbH+fDqKhQ5Nl8H5l4D+gEhKpraHQSigo4vv+L0tUL5"; 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: <4e99fe53$0$281$14726298@news.sunsite.dk> Cancel-Lock: sha1:5s3umLSnhJWJ1tzlwbbNJL2v1IY= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8837 On 10/15/2011 2:42 PM, Arne Vajhøj wrote: > On 10/15/2011 5:36 PM, Arne Vajhøj wrote: >> 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()). >> >> Wrong approach. Write nice clean code. If it is fast enough then >> fine. If not then measure where the bottleneck is. It seems highly >> unlikely to be in getters. >> >>> 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% ) ? >> >> First thing would be to run a lot more than 100000 times. Such >> small intervals will be very random on a multi tasking OS. > > With 1000000 and -server I get: > > 1763 > 1608 > > which is rather close. > this is an interesting result... I was aware that the JVM optimized getters, but unclear is why they are slightly faster than direct field access, rather than exactly the same speed (or sometimes slightly faster and sometimes slightly slower).