php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79856 for() terribly poor performance
Submitted: 2020-07-14 07:36 UTC Modified: 2020-07-14 07:40 UTC
From: bugzilla77 at gmail dot com Assigned: beberlei (profile)
Status: Closed Package: Performance problem
PHP Version: 8.0.0alpha2 OS: Windows 10 Pro
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bugzilla77 at gmail dot com
New email:
PHP Version: OS:

 

 [2020-07-14 07:36 UTC] bugzilla77 at gmail dot com
Description:
------------
for() terribly poor performance

Test script:
---------------
// --- php

<?php
 $t=microtime(true);

 $s=0;
 for($i=0;$i<20000000;$i++){
  $s+=$i;
 }

 $t=microtime(true)-$t;

 print(($t*1000).'ms');
?>



// --- javascript

<script>
 t=new Date();

 s=0;
 for(i=0;i<20000000;i++){
  s+=i
 }

 t=new Date()-t;

 document.write(t+'ms');
</script>



// --- C#

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            int t = Environment.TickCount;

            long s = 0;

            for (int i = 0; i < 20000000; i++)
            {
                s += i;
            }

            t = Environment.TickCount - t;

            Console.WriteLine(t+"ms");
            Console.ReadKey();
        }
    }
}

Expected result:
----------------
Performance at the level of other languages.

Actual result:
--------------
PHP 8.0alpha2: 343ms 
PHP 7.4: 461ms
javascript (Firefox 78.0.2): 58ms
C#: 16ms

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-14 07:40 UTC] beberlei@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: beberlei
 [2020-07-14 07:40 UTC] beberlei@php.net
This is a bad comparison, because the result of the for loop can be calculated by the compiler in C# or an advanced JIT in case of Javascript.

PHP has opcache optimizations, but I don't believe there is one for this for loop construct. PHPs JIT is also in its infancy.

You should check instead calling a function that computes something inside the loop, for example a string operation.

I don't consider this a bug, because of the benchmark assumptions being questionable, so closing.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 20:01:35 2025 UTC