|
1
|
|
|
<?php |
|
2
|
|
|
require __DIR__ . '/../autoload.php'; |
|
3
|
|
|
|
|
4
|
|
|
use Fwlib\Test\Benchmark\Benchmark; |
|
|
|
|
|
|
5
|
|
|
use Fwlib\Util\Common\NumberUtil; |
|
6
|
|
|
use Fwlib\Util\UtilContainer; |
|
7
|
|
|
|
|
8
|
|
|
// Test duplicate times |
|
9
|
|
|
$count = 10000; |
|
10
|
|
|
|
|
11
|
|
|
$bm = new Benchmark(); |
|
12
|
|
|
$bm->setUtilContainer(UtilContainer::getInstance()); |
|
13
|
|
|
|
|
14
|
|
|
$bm->start("Compute $count times"); |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
// Length 20 > 11, will lose precision if use build-in base_convert() |
|
18
|
|
|
/** @noinspection SpellCheckingInspection */ |
|
19
|
|
|
$x = 'abcdef00001234567890'; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
$bm->mark('base_convert()'); |
|
23
|
|
|
for ($i = 0; $i < $count; $i ++) { |
|
24
|
|
|
$y = base_convert($x, 16, 10); |
|
25
|
|
|
} |
|
26
|
|
|
$bm->mark('Base 16 to base 10'); |
|
27
|
|
|
for ($i = 0; $i < $count; $i ++) { |
|
28
|
|
|
base_convert($y, 10, 36); |
|
29
|
|
|
} |
|
30
|
|
|
$bm->mark('Base 10 to base 36'); |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$numberUtil = new NumberUtil; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
if (extension_loaded('bcmath')) { |
|
|
|
|
|
|
37
|
|
|
$bm->mark('baseConvertBcmath()'); |
|
38
|
|
|
|
|
39
|
|
|
for ($i = 0; $i < $count; $i ++) { |
|
40
|
|
|
$y = $numberUtil->baseConvertBcmath($x, 16, 62); |
|
41
|
|
|
} |
|
42
|
|
|
$bm->mark('Base 16 to base 62'); |
|
43
|
|
|
for ($i = 0; $i < $count; $i ++) { |
|
44
|
|
|
$numberUtil->baseConvertBcmath($y, 62, 16); |
|
45
|
|
|
} |
|
46
|
|
|
$bm->mark('Base 62 to base 16'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
View Code Duplication |
if (extension_loaded('gmp')) { |
|
|
|
|
|
|
51
|
|
|
$bm->mark('baseConvertGmp()'); |
|
52
|
|
|
|
|
53
|
|
|
for ($i = 0; $i < $count; $i ++) { |
|
54
|
|
|
$y = $numberUtil->baseConvertGmp($x, 16, 62); |
|
55
|
|
|
} |
|
56
|
|
|
$bm->mark('Base 16 to base 62'); |
|
57
|
|
|
for ($i = 0; $i < $count; $i ++) { |
|
58
|
|
|
$numberUtil->baseConvertGmp($y, 62, 16); |
|
59
|
|
|
} |
|
60
|
|
|
$bm->mark('Base 62 to base 16'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
if (extension_loaded('gmp') && version_compare(PHP_VERSION, '5.3.2', '>=')) { |
|
65
|
|
|
$bm->mark('baseConvertGmpSimple()'); |
|
66
|
|
|
|
|
67
|
|
|
for ($i = 0; $i < $count; $i ++) { |
|
68
|
|
|
$y = $numberUtil->baseConvertGmpSimple($x, 16, 62); |
|
69
|
|
|
} |
|
70
|
|
|
$bm->mark('Base 16 to base 62'); |
|
71
|
|
|
for ($i = 0; $i < $count; $i ++) { |
|
72
|
|
|
$numberUtil->baseConvertGmpSimple($y, 62, 16); |
|
73
|
|
|
} |
|
74
|
|
|
$bm->mark('Base 62 to base 16'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$bm->display(); |
|
79
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: