Alexey Zakhlestin's Blog

Programming for Mac and Web

Regexps in PHP, Again

Permalink

People keep on insisting, that preg_match is better for non-unicode lookups than mb_ereg. So, here are actual benchmarks to make it clear.

Here are results:

1
2
3
preg_match:      19.8039090633
mb_ereg:         15.9386620522
mb_ereg_search:  1.24934506416

Here is the source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
$regexp = '[\w]+@[\w]+\.com';
$pcre_regexp = '/'.$regexp.'/';

$regexp2 = '[\s]+@[\s]+\.com';
$pcre_regexp2 = '/'.$regexp2.'/';

$text = 'blabla bla blbaaasdajkln dsfkl klewnjklfnjkne qwe123@gg.net adkljaskdlnkljnasdljk qwe@test.comasdjlajnsdklnasdklnjl';

$t1 = microtime(true);
for ($i = 0; $i < 100000; $i++) {
    $res1 = preg_match($pcre_regexp, $text);
    $res2 = preg_match($pcre_regexp2, $text);
}
$t2 = microtime(true);

$t3 = microtime(true);
for ($i = 0; $i < 100000; $i++) {
    $res3 = mb_ereg($regexp, $text);
    $res4 = mb_ereg($regexp2, $text);
}
$t4 = microtime(true);

$t5 = microtime(true);
mb_ereg_search_init($text);
for ($i = 0; $i < 100000; $i++) {
    $res5 = mb_ereg_search($regexp);
    $res6 = mb_ereg_search($regexp2);
}
$t6 = microtime(true);


echo 'preg_match:      '.($t2 - $t1)."\n";
echo 'mb_ereg:         '.($t4 - $t3)."\n";
echo 'mb_ereg_search:  '.($t6 - $t5)."\n";

Feel free to check it out yourself.

Comments