[ create a new paste ] login | about

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

PHP, pasted on Oct 26:
<?php
//Serialize
$start = microtime(true);
for($i=0; $i < 1000; $i++) {
    $post = array('str', 'blah', 23, 'undefined', 66, 'abc', 'undefined');
    $post = unserialize(str_replace('s:9:"undefined";', 's:0:"";', serialize($post)));
    unset($post);
}
$end = microtime(true);
$s_time = $end - $start;

//Plain foreach
$start = microtime(true);
for($i=0; $i < 1000; $i++) {
    $post = array('str', 'blah', 23, 'undefined', 66, 'abc', 'undefined');
    foreach($post as &$item) {
       if($item === 'undefined') {
           $item = '';
       }
    }
    unset($post);
}
$end = microtime(true);
$f_time = $end - $start;

if($s_time > $f_time) {
    $s_percent = '';
    $f_percent = '('.intval($s_time / $f_time).'x)';
} else {
    $f_percent = '';
    $s_percent = '('.intval($f_time / $s_time).'x)';
}

printf("Serialize: %s %s\nForeach:   %s %s", $s_time, $s_percent, $f_time, $f_percent);
?>


Output:
1
2
Serialize: 0.011138916015625 
Foreach:   0.0045831203460693 (2x)


Create a new paste based on this one


Comments: