From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 7749 invoked from network); 19 Mar 2021 23:53:06 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 19 Mar 2021 23:53:06 -0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Message-ID:Date: Content-Transfer-Encoding:Content-ID:Content-Type:MIME-Version:Subject:To: References:From:In-reply-to:cc:Reply-To:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=jqex3911ZvOgiaMHHf8XgaGWZxav5GXpCqiSH3XVL6M=; b=ttz+gZpzl19Q1caLNQuoURvFoD 9PDYfjHytFmMKAIva5hmEMJ/hj1Ps7qf0dfT+v1JIiU/3kyIaPosN56lBzyM64q4Tov6+NWv1Sdm7 84liEOpeFUei+8k4O2oXl8kg71cm1Yb0E4OvNhW29kTBvIyUHIVnkKYpT/FkrRiFeZVbUUzj060ZC 6xcIsvDl+n1dDU9q9H62Fb8F5EBmmUrTK2T3x8blhO7S/tc/hjCZ0zZUEyhygKjutGdm9wyxMsJRq oXtFDX5fiy9YYV+SU3S3yMcjdxv/MmbccmNuMfilMn38Y8NFGM3wfQRVZvwoi2QKbwMBTplLvy36d ryyB0qMA==; Received: from authenticated user by zero.zsh.org with local id 1lNOvR-000FBo-QM; Fri, 19 Mar 2021 23:53:05 +0000 Received: from authenticated user by zero.zsh.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1lNOv3-000EnN-JU; Fri, 19 Mar 2021 23:52:41 +0000 Received: from [192.168.178.21] (helo=hydra) by mail.kiddle.eu with esmtp(Exim 4.93.0.4) (envelope-from ) id 1lNOv1-000Al6-VU; Sat, 20 Mar 2021 00:52:40 +0100 cc: Zsh Users In-reply-to: From: Oliver Kiddle References: To: Ray Andrews Subject: Re: run time of math problem MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <41358.1616197959.1@hydra> Content-Transfer-Encoding: 8bit Date: Sat, 20 Mar 2021 00:52:39 +0100 Message-ID: <41359-1616197959.973399@0dqq.OWmi.jXTb> X-Seq: 26585 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Archived-At: Ray Andrews wrote: >     for ((level=1; level<100; level++)); do >         sum= >         for ((terms=level; terms; terms--)); do > >         # 'remainder' calculation done directly here: >         sum+=$(( ( (level - 1.0) / level )**(terms - 1) )) >     done >     divided=$(( sum * (1.0 / level) )) >     echo for level: $level, survival: $divided > done > > ... I'd expect the thing to run a teeny bit faster but in fact it > runs about 15% slower.  Is that explicable?  Does zsh prefer Given that the latter approach has moved the remainder calculation inside a loop and it needs to be repeated 50 times (on average), it should be no surprise that it is slower. If you want to optimise for speed, avoid string conversions and do, e.g. (( divided = sum * (1.0 / level) )) You also may want to make sure to declare some of the variables as float or integer or whatever. The sum+= line might end up being a string concatenation if not. Oliver