note 107475 added to function.str-rot13
| From: | shaun | Date: | Sat, 11 Feb 2012 14:44:13 +0000 |
| Subject: | note 107475 added to function.str-rot13 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186069@lists.php.net to get a copy of this message | ||
I was reminded again of the desire for a generic str_rot function. Character manipulation loops in
PHP are slow compared to their C counterparts, so here's a tuned version of the previous
function I posted. It's 1.6 times as fast, mainly by avoiding chr() calls.
<?php
function str_rot($s, $n = 13) {
static $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$n = (int)$n % 26;
if (!$n) return $s;
if ($n == 13) return str_rot13($s);
for ($i = 0, $l = strlen($s); $i < $l; $i++) {
$c = $s[$i];
if ($c >= 'a' && $c <= 'z') {
$s[$i] = $letters[(ord($c) - 71 + $n) % 26];
} else if ($c >= 'A' && $c <= 'Z') {
$s[$i] = $letters[(ord($c) - 39 + $n) % 26 + 26];
}
}
return $s;
}
?>
But using strtr() you can get something 10 times as fast as the above :
<?php
function str_rot($s, $n = 13) {
static $letters = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';
$n = (int)$n % 26;
if (!$n) return $s;
if ($n < 0) $n += 26;
if ($n == 13) return str_rot13($s);
$rep = substr($letters, $n * 2) . substr($letters, 0, $n * 2);
return strtr($s, $letters, $rep);
}
?>
This technique is faster because PHP's strtr is implemented in C using a byte lookup table (it
has O(m + n) complexity). However, PHP 6 will use Unicode, so I guess(?) strtr will then have to be
implemented with a search for each character (O(m * n)). Using strtr might still be faster since it
offloads the character manipulation to C rather than PHP, but I don't really know. Take your
pick.
Happy coding!
(Benchmark code):
<?php
for ($k = 0; $k < 10; $k++) {
$s = 'The quick brown fox jumps over the lazy dog.';
$t = microtime(1);
for ($i = 0; $i < 1000; $i++) $s = str_rot($s, $i);
$t = microtime(1) - $t;
echo number_format($t, 3) . "\n";
}
?>
----
Server IP: 212.126.36.70
Probable Submitter: 109.255.178.6
----
Manual Page -- http://www.php.net/manual/en/function.str-rot13.php
Edit -- https://master.php.net/note/edit/107475
Del: integrated -- https://master.php.net/note/delete/107475/integrated
Del: useless -- https://master.php.net/note/delete/107475/useless
Del: bad code -- https://master.php.net/note/delete/107475/bad+code
Del: spam -- https://master.php.net/note/delete/107475/spam
Del: non-english -- https://master.php.net/note/delete/107475/non-english
Del: in docs -- https://master.php.net/note/delete/107475/in+docs
Del: other reasons-- https://master.php.net/note/delete/107475
Reject -- https://master.php.net/note/reject/107475
Search -- https://master.php.net/manage/user-notes.php