Skip to content

Instantly share code, notes, and snippets.

@tobiassjosten
Created February 8, 2010 13:19
Show Gist options
  • Save tobiassjosten/298129 to your computer and use it in GitHub Desktop.
Save tobiassjosten/298129 to your computer and use it in GitHub Desktop.
<?php
// $ php -f tostring_helper_benchmark.php
// 0.877526044846 <- Arrays are slooow
// First: 0.352955102921
// Second: 0.354468107224 <- substr() way takes a .0015s penalty
$time = microtime(true);
$html=array();
for ($i=1; $i<1000000; $i++)
{
$html[] = 'asdf';
}
$html = implode(' ', $html);
print (microtime(true)-$time)."\n";
$time = microtime(true);
$html='';
for ($i=1; $i<1000000; $i++)
{
$html .= ' asdf';
}
print 'First: '.(microtime(true)-$time)."\n";
$time = microtime(true);
$html='';
for ($i=1; $i<1000000; $i++)
{
$html .= ' asdf';
}
if (!empty($html))
{
$html = substr($html, 1);
}
print 'Second: '.(microtime(true)-$time)."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment