Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100473 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69967 invoked from network); 8 Sep 2017 15:36:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Sep 2017 15:36:01 -0000 Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.194 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.128.194 mail-wr0-f194.google.com Received: from [209.85.128.194] ([209.85.128.194:35679] helo=mail-wr0-f194.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 29/11-10715-ED8B2B95 for ; Fri, 08 Sep 2017 11:36:00 -0400 Received: by mail-wr0-f194.google.com with SMTP id n64so1408915wrb.2 for ; Fri, 08 Sep 2017 08:35:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-transfer-encoding; bh=+rHJ0sMDNoW+yQGR/osZfEVHCnpJ46HueElahev9EN0=; b=NSOa8ZkCE5cfJ0yB4ex+FHddakt+aVJsvKSpUVcNsWhwxD/j6NdZ9Jz8ate9iXPSqQ qwSTffUrPQh3ca+JhN9uJL160eAbynLFq1pBXglCjIGDtHqAK/tF3KuCX0PPoyzZza/A M6uST3tmJlfwPwr3/cGBVyng8BVkGWv1j22GouuzzVXprjkNU6kDh1sjzXlMlm2Z0vBX pGpL3S/bcp3MaYHa5TdeUMdk2g1nLY741Yk9UaJYDOZfcCOD/nR6lLyj/Gj7jvxa5b8G Pw8CdMSkOL6wV/JaSwjbPUMV7+LjANEBgztIdrbMxCzQSangH6+5rhRQKMLbyaoXBYWT /cPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc:content-transfer-encoding; bh=+rHJ0sMDNoW+yQGR/osZfEVHCnpJ46HueElahev9EN0=; b=lImKhkkSBuwwThC7+3ksUBON+OPsbHHu8oqngvZhklh6JcHwyxFQWb7FvVr6j4dSPW OheBkcxUskexAvLzztyar0jeLOtSYbcmUZeW+g8HfU7862CkjbYBuL6YljxeWOlJYX+Z 9Hd07Pwookku7Dsosc52ZSel/XWVE9VnNLyv8ah42zvZC5QMcbrddpQOnoIzpcD8ZM6E wdr7M0Fft45PQwrGRuj1KxCCB02LaBlmn7aBM5VTSD4vFcC9yMTO2Xg4L5Ox/DCWEJah KRIGyS6xSnZcf5OnN64l01lDiF0rMiW8VQqfX5yUDyVjWoVedDDmmEKdWxXTCt6bdYhZ ZxZQ== X-Gm-Message-State: AHPjjUgkci9YABPfsUSHBiHTyHLDEbWAT+UYSFSH4nbV02Pd5ao7TFnJ Zz4iRHLU7qFZoDGIy1LhBDwpa01DZw== X-Google-Smtp-Source: ADKCNb7+1ILBxweQpaeHo9pby6ancQnxNEhhLILDw3QaJSGLx+PVJNwPrR3hb2VtGbh/V2HYhH33ROiqy5bCKDw7OWA= X-Received: by 10.223.187.5 with SMTP id r5mr2304205wrg.73.1504884955697; Fri, 08 Sep 2017 08:35:55 -0700 (PDT) MIME-Version: 1.0 Sender: morrison.levi@gmail.com Received: by 10.28.193.130 with HTTP; Fri, 8 Sep 2017 08:35:55 -0700 (PDT) In-Reply-To: <1504883189.2374.26.camel@schlueters.de> References: <1504883189.2374.26.camel@schlueters.de> Date: Fri, 8 Sep 2017 09:35:55 -0600 X-Google-Sender-Auth: OnnLO_4U5bGDxjslmg2Vda3gdV8 Message-ID: To: =?UTF-8?Q?Johannes_Schl=C3=BCter?= Cc: David Rodrigues , PHP Internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] array_filter vs for/foreach usage cost From: levim@php.net (Levi Morrison) On Fri, Sep 8, 2017 at 9:06 AM, Johannes Schl=C3=BCter wrote: > Hi, > > On Do, 2017-09-07 at 15:13 -0300, David Rodrigues wrote: >> I understand that array_filter() should costs more than for/foreach >> because >> it is a function call that call another function for each item, while >> the >> for/foreach is a language constructor that works on a way totally >> different >> and its optimized for this kind of job. > > The main cost in that is that array_filter has to create a copy of the > array. An alternative is using an FilterIterator or generator or such > which would make this a pipeline step. (with iterators there are > multiple function calls, while the function descriptor is cached, > making them relatively cheap) I'd focus on improving those. As far as I can tell from the source `array_filter` does not create a copy of the array. It initializes a new array and then appends to it. If we want to improve the performance of `array_filter` we need to examine output of common compilers to see if they hoist the have_callback check out of the loop; I suspect it may not given the complexity of ZEND_HASH_FOREACH_KEY_VAL_IND.