php - preg_replace speed optimisation -


When looking at the accepted answer, the author added + after expression

$ str = preg_replace ('/ [^ 0-9.] + /', '', $ Str);

To remove, instead of single events, + is optional for functionality to find sub-string but I wondered whether + < / Code> Adding is fast or not. (Or is there any difference?)

I think it is fast, due to the short string and memory handling. But I can also understand that more complex regex expressions are slower than simple words.

So when using this technique to remove sub-string, can it try to find large or small sub-string?

Do not read too much in benchmark results, they are incredibly difficult to do well From this, you should take only one thing that the repetition can happen on some types of wires, where the repetition time is long.

PCRE's such things that can easily change with a different version.

 function tst ($ pat, $ str) {$ start = microtime (true); Preg_replace ($ pat, '', $ str); Return Microtim (True) - $ start; } $ Strs = array ('letters' => str_repeat ("a", 20000),' number '=> str_repeat ("1", 20000),' mostly_letters' => str_repeat ("aaaaaaaaaa55", 20000), 'mostly_numbers '=> Str_arapet ("5555555555555a", 20000)); $ Pats = array ('rep' => '/[^0-9.]+/', 'norep' => '/[^0-9.]/'); // precompile patterns (cache them per PHP scripts) and microtime microtime hot (true); Preg_replace ($ pats ['rep'], '', 'foo'); Preg_replace ($ pats ['norep'], '', 'foo'); Foreign currency ($ strs = strname => $ str) {resonant "$ strname \ n"; Forex Currency ($ Pats $ Patada => $ PAT) {printf ("% 10s% .5f \ n", $ Patten, TST ($ pat, $ str)); }} 

Comments

Popular posts from this blog

php - multilevel menu with multilevel array -

c# - TypeConverter in propertygrid only converts from string, not to -

jQuery UI: Datepicker month format -