groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nelson, Erick" <>
Subject Re: Optimising a Groovy script
Date Wed, 29 Mar 2017 13:56:23 GMT
I'm not sure using or not using def would cause performance differences.
It just affects variable scope.

Erick Nelson
Senior Developer
HD Supply, FM
Cell 858-740-6523
Home 760-930-0461

CONFIDENTIALITY NOTICE: This message is for intended addressee(s) only and
may contain information that is confidential, proprietary or exempt from
disclosure, and subject to terms at:

On 3/29/17, 1:19 AM, "Paul Moore" <> wrote:

>On 28 March 2017 at 22:08, Nelson, Erick <>
>> Try this...
>Thanks for the suggestion - there were some nice improvements in here.
>> def rng = new MersenneTwister()
>> def roll = {
>>         rng.nextInt(6) + rng.nextInt(6) + rng.nextInt(6) + 3
>> }
>You changed my definitions to use "def" here. This seems to be the
>thing that makes the most difference in performance. I'm really
>struggling to find a good explanation as to the effect of using or not
>using "def". I had imagined that using "int roll() {..." would be
>better, as it explicitly states the types which would help the
>compiler avoid the need for generic code. Obviously I was wrong, but
>I'm not at all clear why.
>Also, if I use "def rng" but keep "int roll()", I get an error "No
>such property: rng". I'm not clear why that is.
>Do you know of a good resource that explains the difference between
>using def and not doing so? I'm currently working my way through
>"Groovy in Action" and while the subject has been discussed, I didn't
>really follow it. I've also looked at the online docs and they haven't
>helped a lot. It's quite possible that my confusion comes from the
>fact that I only really have a casual knowledge of Java, so the
>precise way classes reference properties and variables isn't clear to
>me - if there's some background reading in Java that would help
>clarify, that would be useful too.
>> int N = 1000000
>> def results = [:].withDefault{0}
>I never knew about withDefault - that's a really nice feature, thanks!
>Thanks for your help,

View raw message