Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100452 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73952 invoked from network); 7 Sep 2017 18:13:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Sep 2017 18:13:54 -0000 Authentication-Results: pb1.pair.com smtp.mail=david.proweb@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=david.proweb@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.169 as permitted sender) X-PHP-List-Original-Sender: david.proweb@gmail.com X-Host-Fingerprint: 209.85.223.169 mail-io0-f169.google.com Received: from [209.85.223.169] ([209.85.223.169:35747] helo=mail-io0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6E/55-10715-16C81B95 for ; Thu, 07 Sep 2017 14:13:53 -0400 Received: by mail-io0-f169.google.com with SMTP id i14so1035496ioe.2 for ; Thu, 07 Sep 2017 11:13:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=pKGwDt7v/Bf1FEQJxd1Q5dnmdE1Xpse9PBXlhSfrRmU=; b=VypueU77gak7Z61coF9hYX53fe98KcjudEPDP81GzhHPUSs/FchnMxpo2OwIpo7PXf 67tTul8s5At8pOGp3+dunK5FD5Sy4mMo4C1vFpDeTSVAZasUApH8pS49ni6hbykDswKG zTeywo0nbipA0RuAYqvuuRdOibgYrs0qnigbp91ftRits1T6ql3AzaDwANx8v3RZmKZl ZhmQdYUjNAA8CrN6YlwLBm4powx6Hb/zAyGZeZCQIBVcO9kfqJrYFha5FWKZj0/RFJzS fzyxrlGd4F9vRKwqhaI5HfZLuMoFeDoH8uawbuUVMYzQzgGG3O2plU31ZjCYwECGZQJj nrwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=pKGwDt7v/Bf1FEQJxd1Q5dnmdE1Xpse9PBXlhSfrRmU=; b=XCe3HuX8I/4ROA4ND1mM0A2VFyArcoqd1YT/7unRc1EmwYDIqqVucZNud0t9suFjiD hG/lu+jqWZkXPTuk3Rh+MwqxO/qwVpeTkzOX7xo3odIPIPCBLoQBYfenCfs1MfAtTHLF bx/t4sxhX5nYcpCNZdy74Nha6LnYKZPLrAz3zOUyooBVZDKNCPpsvf20Mq/oo3MgVwFk BaFBt/1bX5QqXpSQp8wxFkXq20lr2AX3Ww7NNfXVnJIrkybv0FTqjnpqxdsO1mK+UtIY ywpIlSwCTaUigJTBhUIpnX+DGgLr95y1V9nb2+e8ZfP/S5WrHNvIKtZShLgJhCmJTrht JWkA== X-Gm-Message-State: AHPjjUiietqt4gRQ8yt5/bJid483QvF/kpp01oqKjixJA/8YtT6ZcoDQ WVfqZHQ7gxe4ZpDZYXOHtklRXpIdEPhq X-Google-Smtp-Source: AOwi7QD5cMgxamKgCSdBwG05dYbruCPecugrx1p1thQfEcXGi4BqbwAJ3JSoyWzk9eG8f9/XiwCY6i+7DGwQTf/0Ss8= X-Received: by 10.107.70.21 with SMTP id t21mr244502ioa.50.1504808030060; Thu, 07 Sep 2017 11:13:50 -0700 (PDT) MIME-Version: 1.0 Received: by 10.2.36.12 with HTTP; Thu, 7 Sep 2017 11:13:29 -0700 (PDT) Date: Thu, 7 Sep 2017 15:13:29 -0300 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="089e082689a01644eb05589d6cf6" Subject: array_filter vs for/foreach usage cost From: david.proweb@gmail.com (David Rodrigues) --089e082689a01644eb05589d6cf6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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. foreach($items as $item) { if(expr($item)) { $result[] =3D $item } } array_filter($items, function($item) { return expr($item) } ) But there are some possibility to create some runtime optimizer for cases like that, and "understand" that array_filter() should works like for/foreach? JS have some kind of optimization and functions is faster than constructor for cases like that: http://jsben.ch/pZmLf. I have done some benchmarks with a 60 seconds cycle. https://pastebin.com/zGmE4pxm - for/foreach -> 17.06 mi cycles https://pastebin.com/5E2VwHYm - array (prepared function) -> =E2=80=8B5.49 mi cycles (-67.82%)=E2=80=8B https://pastebin.com/jSA9ZBqt - array (prepared var function) -> 3.30 mi cycles (-80.66%) https://pastebin.com/YPdCmphJ - array_filter (inline) -> 3.14 mi cycles (-81.58%) I think that array_filter() cost will keep the same, once that it is a function and I belive that it is already optimized, but the callable could be optimized to "not be a function internally", working like a constructor. And maybe it could be applied to other functions like array_map(). --=20 David Rodrigues --089e082689a01644eb05589d6cf6--