tugwilson | 9 May 18:06
Picon
Favicon

Re: [groovy-user] dif between def obj = new MyObject() vs MyObject obj = new MyObject()


Ed Clark-2 wrote:
> 
> tugwilson wrote:
>>
>> Ed Clark-2 wrote:
>>   
>>> Just curious - are there any performance benefits to specifying the
>>> type, either compile time or run time?  I know that some languages
>>> can speed things up if given some type hints.  (Given the extra
>>> dynamic nature of Groovy, there may be none.)
>>>
>>>     
>>
>>
>> Until very recently using static typing frequently made the program
>> slower.
>>   
> 
> Wow, I didn't expect that answer.  How recent is "very recently"?
> is there a penalty in 1.5.6?  Was this due to things like extra type
> checking on assignment?
> 
>> Now the Groovy runtime is getting faster it's likely that in future using
>> static typing (especially if the type is a primitive) will make the
>> program
>> run faster. I have an experimental dynamic runtime which executes
>> expressions almost twice as fast if the types of the variables are known
>> and
>> are primitive,
>>
>>   
> Anything targeted to 1.6?  Just wondering whether to start being
> more thorough in specifying types.  Although I had said that I
> think it's a good idea to specify types, I'm also quite lazy ;-) and
> tend to leave them off if there's no performance payoff (especially
> if there's a penalty).
> 

It may still be true that specifying types can slow down a program - I
haven't benchmarked this in a while. The overhead comes at assignment where
you have to cast or convert the Object result of the expression to the
target type of the assignment. You also get boxing/unboxing costs for
primitive types.

I would not be over concerned about this if I were you. There are lots of
gains to be made in optimising the runtime system so your programs will get
faster without you having to "optimise" them yourself. At some point the
increased speed will begin to make this overhead a larger proportion of the
execution time of your program and you can begin to worry about tuning the
5%-10% of your code which will make a difference.

Personally I very rarely type a variable unless it's part of the interface
to some Java code.

John Wilson
--

-- 
View this message in context: http://www.nabble.com/dif-between-def-obj-%3D-new-MyObject%28%29-vs-MyObject-obj-%3D-new-MyObject%28%29-tp17031075p17151205.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane