PHP Classes

File: benchmark_arraymap_foreach.php

Recommend this page to a friend!
  Classes of Jorge Castro   PHP Benchmarks   benchmark_arraymap_foreach.php   Download  
File: benchmark_arraymap_foreach.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Benchmarks
Evaluate the speed of PHP running different tasks
Author: By
Last change:
Date: 3 years ago
Size: 1,322 bytes
 

Contents

Class file image Download
<?php

$numbers
= range(0, 1000);

include
"Collection.php";

$instances=5000;

// **********************************************************************************
$t1=microtime(true);
for(
$i=0;$i<$instances;$i++) {
   
$result = array();
    foreach (
$numbers as $number) {
       
$result[] = $number * 10;
    }
}
$t2=microtime(true);
$table['foreach']=$t2-$t1;

// **********************************************************************************
$t1=microtime(true);
for(
$i=0;$i<$instances;$i++) {
   
$result = array_map(function ($number) {
        return
$number * 10;
    },
$numbers);
}
$t2=microtime(true);
$table['array_map']=$t2-$t1;

// **********************************************************************************
$t1=microtime(true);
for(
$i=0;$i<$instances;$i++) {
   
$result = array_map(static function ($number) {
        return
$number * 10;
    },
$numbers);
}
$t2=microtime(true);
$table['array_map (static)']=$t2-$t1;

// **********************************************************************************
$t1=microtime(true);
function
tenTimes($number)
{
    return
$number * 10;
}
for(
$i=0;$i<$instances;$i++) {
   
$result = array_map('tenTimes', $numbers);
}
$t2=microtime(true);
$table['array_map (calling a function)']=$t2-$t1;


echo \
mapache_commons\Collection::generateTable($table);