-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_preg_match.php
39 lines (32 loc) · 1.01 KB
/
test_preg_match.php
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
36
37
38
39
<?php
// require vender autoload
require_once('vendor/autoload.php');
// add namespace at the top
use Performance\Performance;
// preparation
$a = [...range('a','z'), ...range('a','9'), ...range('a','Z'), ...range('z','A')];
for ($i=0; $i < 10; $i++) {
$a = [...$a,...$a];
}
// obvious method
Performance::point('preg_match');
foreach ($a as $s) {
$result = preg_match('/[a-zA-Z0-9]+/', $s);
}
Performance::finish();
// alternative method
Performance::point('ctype_alnum');
foreach ($a as $s) {
$result = ctype_alnum($s);
}
Performance::finish();
// finish all tasks and show test results
Performance::results();
$export = Performance::export();
$points = json_decode($export->toJson())->points;
$p2 = end($points);
$p1 = prev($points);
$percent = round(1-$p2->differenceTime/$p1->differenceTime,4)*100;
$times = round($p1->differenceTime/$p2->differenceTime, 1);
print_r('Alternative method is ' . $percent . '% (' . $times . 'x) faster' . "\n");
print_r('Generated test array with ' . count($a) . ' elements' . "\n");