3

I have a curiosity. I created a simple php script that creates an array of 1 mil simple array elements and then loops through them.

On the first execution, it seems to take about ~1,4 seconds. But on the second execution of the same code, it always takes about ~2,1 seconds. I've repeated this several times with the same results.

Why is this?

Code sample here:

    $timeStart = microtime(true);

    $invoices = array();
    for ($i = 1; $i <= 1000000; $i++) {
        $invoices[] = array(
            'issuedValue' => $i,
            'fiscalNumber' => $i,
            'random1' => $i,
            'random2' => $i,
            'random3' => $i,
        );
    }
    foreach ($invoices as $invoice) {
        // nothing here
    }

    var_dump(microtime(true) - $timeStart);

    // second iteration here

    $timeStart = microtime(true);

    $invoices = array();
    for ($i = 1; $i <= 1000000; $i++) {
        $invoices[] = array(
            'issuedValue' => $i,
            'fiscalNumber' => $i,
            'random1' => $i,
            'random2' => $i,
            'random3' => $i,
        );
    }
    foreach ($invoices as $invoice) {
        // nothing here
    }

    var_dump(microtime(true) - $timeStart);
2
  • I guess you'd say the "// something something" is identical in both cases? My view would be that it would be in there... as there is nothing obvious in the for loop. Also please run the same test multiple times and take an average... other items could be affecting the speed. Commented Jun 16, 2016 at 9:43
  • Try running your code in loop and see results, maybe some additional processes that makes your PHP slower. Commented Jun 16, 2016 at 11:02

1 Answer 1

4

It happens because of memory usage which also increases the chance that garbage collection cycles trigger during the second run. If you add the following code between runs:

unset($timeStart, $invoices, $i, $invoice);
gc_collect_cycles();

to remove references and clean unused memory, you'll get the same time.

Sign up to request clarification or add additional context in comments.

5 Comments

That closes the gap for me with less iterations, but the times still differ.
How much different? It is about the same on my machine most of the time: 1.2779951095581 and 1.2829298973083.
With fewer iterations you have higher chances your process wasn't touched by the scheduler during its run.
indeed, if i unset the variables (even without forcing garbage collection), this closes the gap but times are still noticeably different . 1.5098850727081 on the first run and 1.2648339271545 on the second
I don't know how to explain this. I've just run this script 15 times and got almost the same results for both runs. In average second run is only slightly faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.