Skip to content

Instantly share code, notes, and snippets.

@tankist
Created December 16, 2010 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tankist/743578 to your computer and use it in GitHub Desktop.
Save tankist/743578 to your computer and use it in GitHub Desktop.
<?php
function rand_str($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
// Length of character list
$chars_length = (strlen($chars) - 1);
// Start our string
$string = $chars{rand(0, $chars_length)};
// Generate random string
for ($i = 1; $i < $length; $i = strlen($string))
{
// Grab a random character from our list
$r = $chars{rand(0, $chars_length)};
// Make sure the same two characters don't appear next to each other
if ($r != $string{$i - 1}) $string .= $r;
}
// Return the string
return $string;
}
$cycles = 10000;
$a = $endTime = array();
for($i=0;$i<=1000;$i++){
array_push($a, rand(0, 10000));
array_push($a, rand_str());
}
shuffle($a);
$b = array();
$time = microtime(true);
for($i=0;$i<=$cycles;$i++){
$b = serialize($a);
}
$endTime['serialize'] = microtime(true) - $time;
$b = array();
$time = microtime(true);
for($i=0;$i<=$cycles;$i++){
$b = json_encode($a);
}
$endTime['json'] = microtime(true) - $time;
$b = array();
$time = microtime(true);
for($i=0;$i<=$cycles;$i++){
$b = print_r($a, true);
}
$endTime['print_r'] = microtime(true) - $time;
var_dump($endTime);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment