Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created April 11, 2012 11:34
Show Gist options
  • Select an option

  • Save sasezaki/2358780 to your computer and use it in GitHub Desktop.

Select an option

Save sasezaki/2358780 to your computer and use it in GitHub Desktop.
to get length of binary string in bytes - tiny benchmark
<?php
$count = 100;
$data = "あいうえお";
for ($i= 1; $i < 10; $i++) {
$data .= $data;
}
echo phpversion(), PHP_EOL;
echo "func_overload ?", PHP_EOL;
var_dump(function_exists('mb_internal_encoding') &&
(((int)ini_get('mbstring.func_overload')) & 2));
echo "show target data length : ", (strlen(bin2hex($data)) / 2), PHP_EOL;
echo "******************** start strlen(bin2hex", PHP_EOL;
$start = microtime();
for ($i= 1; $i < $count; $i++) {
strlen(bin2hex($data)) / 2;
}
$end = microtime();
var_dump($end - $start);
echo "******************** start Util::strlen", PHP_EOL;
$start = microtime();
for ($i= 1; $i < $count; $i++) {
Util::strlen($data);
}
$end = microtime();
var_dump($end - $start);
echo "******************** start Util2::strlen", PHP_EOL;
$start = microtime();
for ($i= 1; $i < $count; $i++) {
Util2::strlen($data);
}
$end = microtime();
var_dump($end - $start);
class Util
{
static public function strlen($data)
{
if (function_exists('mb_internal_encoding') &&
(((int)ini_get('mbstring.func_overload')) & 2)) {
return mb_strlen($data, '8bit');
} else {
return strlen($data);
}
}
}
class Util2
{
static public function strlen($data)
{
static $overload;
if ($overload === null) {
if (function_exists('mb_internal_encoding') &&
(((int)ini_get('mbstring.func_overload')) & 2)) {
$overload = true;
} else {
$overload = false;
}
}
return ($overload) ? mb_strlen($data, '8bit') : strlen($data);
}
}
5.3.5-1ubuntu7.7
func_overload ?
bool(false)
show target data length : 7680
******************** start strlen(bin2hex
float(0.004369)
******************** start Util::strlen
float(0.002455)
******************** start Util2::strlen
float(0.001262)
5.4.0
func_overload ?
bool(false)
show target data length : 7680
******************** start strlen(bin2hex
float(0.0041910000000001)
******************** start Util::strlen
float(0.000552)
******************** start Util2::strlen
float(0.00010500000000002)
@marc-mabe

Copy link
Copy Markdown

Why not simply:

static public function strlen($data) {
static $mbStrlen = function_exists('mb_strlen');
return $mbStrlen ? mb_strlen($str, '8bit') : strlen($str);
}

@sasezaki

Copy link
Copy Markdown
Author

@marc-mabe

as I mentioned past, zendframework/zendframework#546,
above checking environment code is borrowed from Zend\OpenId's.

  • OpenId's "strlen" is coding by Padraic, so I just following it blindly :-)

In addition, If checking only function_exists("mb_strlen"),
all mbstring loaded system not be enforced mb_strlen?
mb_strlen($data, '8bit') is cost than strlen.

And, mbstring.func_overload 's Changeable is PHP_INI_SYSTEM | PHP_INI_PERDIR.
http://php.net/manual/en/mbstring.configuration.php
when checking system strictly,
it can be memorize to static as enforce using mb_strlen($data, '8bit').
(mb_strlen function can define as user function
https://github.com/cakephp/cakephp/blob/master/lib/Cake/I18n/Multibyte.php#L59 ← oh! )

By the way, your suggestion code got syntax error?
"static $mbStrlen = function_exists("mb_strlen");" //Parse error: syntax error, unexpected '(',
It seems static var can't assign functions'return direct.

@marc-mabe

Copy link
Copy Markdown

You are right - The only point I mean is that a static buffered check against "mb_strlen" could be enough to use "mb_strlen" or "strlen".
What is the performance difference of "mb_strlen($str, '8bit')" against "strlen($str)" ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment