<?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);
?>