Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id F0FFC200C46 for ; Wed, 29 Mar 2017 23:03:48 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id EF700160B8A; Wed, 29 Mar 2017 21:03:48 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 3F5BE160B5D for ; Wed, 29 Mar 2017 23:03:48 +0200 (CEST) Received: (qmail 38019 invoked by uid 500); 29 Mar 2017 21:03:47 -0000 Mailing-List: contact users-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.apache.org Delivered-To: mailing list users@groovy.apache.org Received: (qmail 38009 invoked by uid 99); 29 Mar 2017 21:03:47 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Mar 2017 21:03:47 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id E2EF5C002B for ; Wed, 29 Mar 2017 21:03:46 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -2.397 X-Spam-Level: X-Spam-Status: No, score=-2.397 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-2.796, RCVD_IN_SORBS_SPAM=0.5, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd4-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id uhUtwVcdZSGo for ; Wed, 29 Mar 2017 21:03:44 +0000 (UTC) Received: from mail-wr0-f173.google.com (mail-wr0-f173.google.com [209.85.128.173]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id CECBC5FCD9 for ; Wed, 29 Mar 2017 21:03:43 +0000 (UTC) Received: by mail-wr0-f173.google.com with SMTP id w11so32045302wrc.3 for ; Wed, 29 Mar 2017 14:03:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=VagDdtryV9RVZO+vx7yC7eH/rT0RAVtHfzePqHMvjyA=; b=hJPtucAFxk7fM8tunttFtebB5B3YfQBQ5kzc/0dVONrA+BFHy2iV7595BA0cQLoeSC joIBibn5lt58T+KcVNg+SDYHAlGtOsETpjjDGRPLqb1apRV4Pzd1xt5LFQP9jArc+Uxo y4Zhn/pb3pcqMpf1rQv2+Edns018WIwA4Xf9go8Qmgttu1D5zs07zCRLK1OvxC0RZB1n 9DcTtsvQr1jsOPr8BmkfneSGLyQsXCt+yZOW5jZ15nYpg5tdkL3fGtkvdKUYDm4mp3DK 48eyTSIVruWrCiqa264ivHnfshTo60hXA+B2/NY1XauaufWb1yJTr66kO/v6hG6GIMNe T90A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=VagDdtryV9RVZO+vx7yC7eH/rT0RAVtHfzePqHMvjyA=; b=dQzKUjUvrkBplJKUYz9Z9cV30tQfGAB20tpiji9LURAQ3XYz4ZHReLzHwonV17gMv4 96GEClNRuQj6NJnfVI8jDgYVf2ShCt0QAmWa22fEASl79OZ/B/8/oHHwuXoo0uO1aj4V 8UmGnoUU+nCu7tQfM/kjPaaLg2OR1NWuWX5KPMCcatqxcTtkwChaWsq5D26RuH1t7Qix igZ0/0meqBkqtEUixxyN5C9s5DdIbMlxuHOBI7oIq95gfg2Pmo/zik4mawj0FIakFI89 AlxSQO9pCGvZyCK67IZ7HxtMcMq/IA3zz65C5AmtaDTAjfEs4uiZZ+9alU1tXFPpHAaC CIlA== X-Gm-Message-State: AFeK/H2tlrsZEQTokS/vh9x7G4f4HziHXpdzNRkmtXCMeiV8MxwDOb+O/D2uIQVuOx5J+/l45hHkBeZ0ZZp3uA== X-Received: by 10.223.150.168 with SMTP id u37mr2394999wrb.184.1490821423200; Wed, 29 Mar 2017 14:03:43 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.179.88 with HTTP; Wed, 29 Mar 2017 14:03:42 -0700 (PDT) In-Reply-To: <6EC35B64-B87E-4B3C-93B2-B082016DAB50@anc.org> References: <6EC35B64-B87E-4B3C-93B2-B082016DAB50@anc.org> From: Paul Moore Date: Wed, 29 Mar 2017 22:03:42 +0100 Message-ID: Subject: Re: Optimising a Groovy script To: Groovy Users Content-Type: text/plain; charset=UTF-8 archived-at: Wed, 29 Mar 2017 21:03:49 -0000 On 29 March 2017 at 20:54, Keith Suderman wrote: > Two optimizations I have not seen mentioned so far; don't be so Groovy ;-) :-) My background is Python, so I tend to think in terms of highly dynamic code. That of course is why I like Groovy ;-) > 1. Replace the the Map<> with an array of primitive ints. Why use an integer > as a key into a hash map when it can be used as an array index? > int[] results = new int[19] // since we need to index values 3..18 Two reasons, which I did mention, but only in in passing. I don't necessarily know the range of outputs (so a fixed upper bound may not be appropriate), and in some cases the result may not even be an int. I could special-case those situations, but I'd prefer to get as much out of a general design as I can before going down that route. > 2. Replace the N.times{} or (1..N).each{} loops with a good old fashioned > for(int i=0;i improvement this made if @CompileStatic was not used. For statically > compiled code this didn't really make a difference, but for dynamic code the > speed up is huge. I'll definitely try that! The roll() function will ultimately (in my final version) be supplied by the user, so I don't want to impose too many restrictions on what's allowed there. I'm OK with making the user responsible for a certain level of tuning the code, but a key goal for me is to make the way the user specifies the function to simulate as straightforward as possible. > Of course, the big winner is using @CompileStatic, which when combined with > using an array results in a ~10x speed up. Yep, those figures are impressive. I'll take a good look at @CompileStatic - I've not really looked at it much yet. From what I've read, it imposes some restrictions on the code, but I don't think the restrictions will be a problem for my situation. One thought, though - if the function being simulated is supplied by the user, would it have to be the user's responsibility to include the @CompileStatic annotation? I assume it's not possible for my simulation function to take the user's code as input, and apply @CompileStatic to it before running it? Even if I don't want a solution that reads code at runtime, I'd still probably like a function that took the user's code as a closure - something like "simulate(1000000) { user code here }" but again I assume that means I can't use @CompileStatic? The array definitely seems like a win, and as it's something I can do in the driver code without affecting the user code, I may look again at that. Maybe I'll try a hybrid approach that uses an array for most values with a map to hold "outliers" (numbers bigger than I expect and non-numbers). That may lose the benefit, though - the only way to know for sure is to measure. Thanks for the suggestions. Paul