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 A41B52009A8 for ; Tue, 17 May 2016 11:02:12 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A2A0D1609F5; Tue, 17 May 2016 09:02:12 +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 EBB6E1609AE for ; Tue, 17 May 2016 11:02:11 +0200 (CEST) Received: (qmail 33443 invoked by uid 500); 17 May 2016 09:02:10 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 33431 invoked by uid 99); 17 May 2016 09:02:10 -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; Tue, 17 May 2016 09:02:10 +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 3F4A6C0DD8 for ; Tue, 17 May 2016 09:02:10 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.802 X-Spam-Level: X-Spam-Status: No, score=-0.802 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, 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 mx2-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 5cwHMobe7dAh for ; Tue, 17 May 2016 09:02:09 +0000 (UTC) Received: from mail-io0-f182.google.com (mail-io0-f182.google.com [209.85.223.182]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with ESMTPS id 7AF625F4E3 for ; Tue, 17 May 2016 09:02:08 +0000 (UTC) Received: by mail-io0-f182.google.com with SMTP id d62so15063642iof.2 for ; Tue, 17 May 2016 02:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=t/xfVOl9nYsGdooIrm0dTmu6ycStkJPBkRyJdAmMDpk=; b=u5C8/fvPqF6BeB2wj/o/3AWRi7vUEdjp1zKUxx8yJzmWdYIIw90dOB9DxlwXHkO5VN 5oFOTh8nqRy/xPLq6qGZgFh5yTbW0zu40BEgqbkPRaShtlVs0+NzonFdSq8VatrmBFEz s5kANwug3LMrPRWfBJ1mPffLFo3yanvYkDhFuRtalWoS4VVmvSvoshZ6rw4rwCRbwnyP Wjy5jrS7ZVA7ar1vYiQuEBWdIXXYcyL8sJXRmONQU3ajTmyRg8Mkfwn41UXcnZezF/wy tIVwxrecZ8J0nqlcHkwESMHd/B1MBYYaCXaF+sM+2NFc6r6VewwKOKtZNtgqjTM+Cd9W ROZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=t/xfVOl9nYsGdooIrm0dTmu6ycStkJPBkRyJdAmMDpk=; b=fFyfuhVEWy3lUAZNcLyFvt+3uXbso+XsxNogllNcq/hZaUU5BvkmhCd3AKwHVfmOWj HBlXdrF8iR/09VDrAEEBjP8qppFaAXHDuMb+/3fVchJpJBoknU6EhAf4JF9QWvg2PfUW YG90q/JQjaQf7kL5IE0cZjEx0jwSwG+pNrr43eI9AfpT2l74z/usj9YPwBFHwiK61zYH v2Y5ozgNMYv8wW5RQQ20WsF0yON7nCqNSCadD3miyHIt2DdoVBVm8KqIQGcAfBW8n7cs I1ZkAEahm1Hb9H6EQz5H78DzcxLSfOgnBXGGyWuPyKPcJIOGESJdvLExCw/tz9jrKKOM sqtQ== X-Gm-Message-State: AOPr4FVDguZx6EiyXawfxIvBDmxL6lQEm8055XGmI8cVuxpivltkSMUVnDmt+AmmxBUD9ulHiXL2yQzdPFmUZg== MIME-Version: 1.0 X-Received: by 10.36.101.16 with SMTP id u16mr12977084itb.33.1463475727418; Tue, 17 May 2016 02:02:07 -0700 (PDT) Received: by 10.107.148.79 with HTTP; Tue, 17 May 2016 02:02:07 -0700 (PDT) In-Reply-To: References: Date: Tue, 17 May 2016 10:02:07 +0100 Message-ID: Subject: Re: [ALL] Performance of foreach loop with arrays. From: sebb To: Commons Developers List Content-Type: text/plain; charset=UTF-8 archived-at: Tue, 17 May 2016 09:02:12 -0000 On 17 May 2016 at 06:04, Eric Barnhill wrote: > I have a related story. There is a class in commons-math 4.0 called > IntegerRange. Stylistically I much prefer Iterating over such a range than > C style loops and was ready to convert. However I found it many times > slower, at least in not terribly rigorous home micro-benchmarking. > > Any good reason why this should be so? This is off-topic for the thread, which is about *arrays*. Please start a new thread. > On 16 May 2016 15:14, "sebb" wrote: > >> Looking for feedback on C-style and foreach() loops when using arrays. >> [This email does not consider collections; please start another thread >> if you want to discuss that.] >> >> TLDR; Always use foreach() with arrays unless you need access to the index. >> >> == >> >> There are two main ways to iterate over an array: >> >> for(int i=0; i < array.length; i++ ) { // C-style >> doSomething(array[i]); >> } >> >> for(Object item : array) { // generic foreach >> doSomething(item); >> } >> >> The JVM has special instructions to load/store items from arrays; >> different instructions are used for Objects and each primitive type; >> these instructions only require the array start address and the index. >> >> There have been suggestions that the for loop is less efficient >> because it creates an iterator which needs to be garbage collected. >> >> AFAICT in the case of *arrays* there is absolutely no need for the >> compiler to create an iterator (or indeed any other temporary object) >> in order to support the foreach() loop. >> >> Experiments with javap confirm this. >> >> IMO the only reason to prefer the C-style loop is where the index is >> needed for something else in the loop. >> >> Otherwise the foreach() loop seems much better to me: >> - simpler to code and read >> - cannot accidentally use the wrong index or wrong index comparison >> >> Also the C-style loop evaluates the index each time through the loop; >> one needs to code it as below to avoid this (the optimiser may do this >> anyway): >> >> int len = array.length; // take array length out of loop >> for(int =0i; i < len; i++ ) { // C-style >> doSomething(array[i]); >> } >> >> This is even messier and more prone to error. >> >> Note: javap does show an extra astore/aload instruction pair in the >> test loop prior to calling doSomething() in the foreach() case. Those >> appear to be superfluous and will presumably be optimised away at >> runtime. A later compiler will likely fix this. In any case the >> overhead of two local memory accesses is miniscule. >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org >> For additional commands, e-mail: dev-help@commons.apache.org >> >> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org