-
Notifications
You must be signed in to change notification settings - Fork 7
/
StrposVsStrstr.php
70 lines (57 loc) · 1.4 KB
/
StrposVsStrstr.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Check what is faster strstr or strpos
*
* @file StrposVsStrstr.php
*
* PHP version 5.4+
*
* @author Yancharuk Alexander <alex at itvault dot info>
* @date Wed Oct 2 16:42:55 2013
* @license The BSD 3-Clause License
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
*/
namespace Tests;
use Application\TestApplication;
use Veles\Tools\CliProgressBar;
use Veles\Tools\Timer;
/**
* Class StrposVsStrstr
* @author Yancharuk Alexander <alex at itvault dot info>
*/
class StrposVsStrstr extends TestApplication
{
protected $repeats = 10000;
public function run()
{
$repeats = $this->getRepeats();
$string = 'This is test string';
$needle = 'is test';
$bar = new CliProgressBar($repeats);
for ($i = 1; $i <= $repeats; ++$i) {
Timer::start();
strpos($string, $needle);
Timer::stop();
$bar->update($i);
}
$this->addResult('strpos', Timer::get());
$bar = new CliProgressBar($repeats);
Timer::reset();
for ($i = 1; $i <= $repeats; ++$i) {
Timer::start();
strstr($string, $needle);
Timer::stop();
$bar->update($i);
}
$this->addResult('strstr', Timer::get());
$bar = new CliProgressBar($repeats);
Timer::reset();
for ($i = 1; $i <= $repeats; ++$i) {
Timer::start();
substr($string, 0, strlen($needle));
Timer::stop();
$bar->update($i);
}
$this->addResult('substr+strlen', Timer::get());
}
}