Skip to content

Instantly share code, notes, and snippets.

@andrewnicols
Created January 12, 2012 16:01
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 andrewnicols/1601304 to your computer and use it in GitHub Desktop.
Save andrewnicols/1601304 to your computer and use it in GitHub Desktop.
<?php
/**
* This example compares mb_string and iconv_string in the way that they're
* used in moodle's break_up_long_words() function. That is to say that it
* runs in a loop taking each character in turn
*/
set_time_limit(0);
// An RSS feed description may realistically hit 5,000 characters
$text = str_repeat('X', 5000);
$len = 1;
$charset = 'utf-8';
$strlen = strlen($text);
/**
* Check mb_substr
*/
echo "<p>Checking mb_substr with a length of $strlen</p>";
$timestart = microtime(true);
for ($i = 0; $i < $strlen; $i++) {
mb_substr($text, $i, $len, $charset);
}
$timeend = microtime(true);
$mbstringtime = $timeend - $timestart;
echo "<p>Took $mbstringtime seconds to process {$strlen} characters. That's " . ($mbstringtime / $strlen) . " seconds per character</p>";
/**
* Check iconv_substr
*/
echo "<p>Checking iconv_substr with a length of $strlen</p>";
$start = 0;
$timestart = microtime(true);
for ($i = 0; $i < $strlen; $i++) {
iconv_substr($text, $i, $len, $charset);
$start++;
}
$timeend = microtime(true);
$iconvstringtime = $timeend - $timestart;
echo "<p>Took $iconvstringtime seconds to process {$strlen} characters. That's " . ($iconvstringtime / $strlen) . " seconds per character</p>";
$totaltime = abs($iconvstringtime - $mbstringtime);
echo "<p>Difference of {$totaltime} seconds - " . ($totaltime / $strlen) . " seconds per character</p>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment