[ create a new paste ] login | about

Link: http://codepad.org/BwBLpmgp    [ raw code | output | fork ]

PHP, pasted on Dec 5:
<?php
$array = range(1, 1000);

//===//

$result = '';

$start = microtime(true);
while($pop = array_pop($array)) {
    $result = "$pop$result";
}
$end = microtime(true);

$time = $end - $start;
echo "array_pop\nTime: $time\n\n";


//===//

$result = '';

$start = microtime(true);
while($shift = array_shift($array)) {
    $result .= $shift;
}
$end = microtime(true);

$time = $end - $start;
echo "array_shift\nTime: $time\n\n";

//===//

$result = '';

$start = microtime(true);
for($i = count($array); $i > 0; --$i) {
    $result = $array[$i] . $result;
}
$end = microtime(true);

$time = $end - $start;
echo "for loop\nTime: $time\n\n";

//===//

$result = '';

$start = microtime(true);
$result = vsprintf(str_repeat("%s", count($array)), $array);
$end = microtime(true);

$time = $end - $start;
echo "vsprintf\nTime: $time\n\n";

//===//

$result = '';

$start = microtime(true);
$result = implode($array);
$end = microtime(true);

$time = $end - $start;
echo "implode\nTime: $time\n\n";

//===//

$result = '';

$start = microtime(true);
$result = str_replace(array("]","[","\"",","), "", json_encode($array));
$end = microtime(true);

$time = $end - $start;
echo "str_replace\nTime: $time\n\n";


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
array_pop
Time: 0.0014219284057617

array_shift
Time: 0.00010895729064941

for loop
Time: 0.0004279613494873

vsprintf
Time: 0.00033998489379883

implode
Time: 0.00016212463378906

str_replace
Time: 0.0012540817260742



Create a new paste based on this one


Comments: