Created
December 17, 2010 09:20
-
-
Save Majkl578/744695 to your computer and use it in GitHub Desktop.
static callback vs. create_function (for Lopo@nettechat)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Foo | |
{ | |
public static function testA($value, $prefix = '') | |
{ | |
self::$substitutePrefix = $prefix; | |
return preg_replace_callback('#:([^:\s]*):#', array(__CLASS__, 'testCb'), $value); | |
} | |
private static $substitutePrefix; // workaround for f***ing PHP <5.3 | |
private static function testCb($m) | |
{ | |
return ''; | |
} | |
public static function testB($value, $prefix = '') | |
{ | |
return preg_replace_callback('#:([^:\s]*):#', create_function('$m, $prefix='."'$prefix'", 'return "";'), $value); | |
} | |
} | |
$xts = microtime(TRUE); | |
for ($i = 0; $i < 20000; $i++) { | |
Foo::testA(':foo:bar', 'x'); | |
} | |
$xts = microtime(TRUE) - $xts; | |
$yts = microtime(TRUE); | |
for ($i = 0; $i < 20000; $i++) { | |
Foo::testB(':foo:bar', 'x'); | |
} | |
$yts = microtime(TRUE) - $yts; | |
var_dump($xts, $yts); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment