-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConcatTest.php
98 lines (86 loc) · 2.7 KB
/
ConcatTest.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Created by PhpStorm.
* User: root
* Date: 18/5/18
* Time: 下午9:31
* 几种字符串拼接的测试
*
* 最快字符串拼接方式 . , .= , sprintf , join, implode, concat
*/
use PHPUnit\Framework\TestCase;
final class ConcatTest extends TestCase
{
private $Count = 10000;
private $reTryTime = 10;
private $tempStr = '19826318654817aasdasdadasd';
public function testDotEqual()
{
$timer = Benchmark::getInstance();
$timeAVG = $timer->tryManyTimes(function () {
$result = "";
for ($i = 0; $i < $this -> Count; $i++) {
$result .= $this->tempStr;
}
}, $this -> reTryTime);
echo "time By .= is : ".$timeAVG."\n";
}
// public function testDouhao()
// {
// $timer = Benchmark::getInstance();
// $timeAvg = $timer -> tryManyTimes(function () {
// $result = "";
// for ($i = 0; $i < $this -> Count;$i++) {
// $result = $result.$this -> tempStr;
// }
// }, $this -> reTryTime);
// echo "time By , is : ".$timeAvg."\n";
// }
public function testDot()
{
$timer = Benchmark::getInstance();
$timeAvg = $timer -> tryManyTimes(function () {
$result = "";
for ($i = 0; $i < $this -> Count;$i++) {
$result = $result.$this -> tempStr;
}
}, $this -> reTryTime);
echo "time By . is : ".$timeAvg."\n";
}
public function testSprintf()
{
$timer = Benchmark::getInstance();
$timeAvg = $timer -> tryManyTimes(function() {
$result = "";
for ($i = 0;$i < $this -> Count;$i++) {
$result = sprintf("%s%s", $result, $this -> tempStr);
}
}, $this -> reTryTime);
echo "time By sprintf() is : ".$timeAvg."\n";
}
public function testVsprintf()
{
$timer = Benchmark::getInstance();
$timeAvg = $timer -> tryManyTimes(function() {
$arg = [];
for ($i = 0;$i < $this -> Count;$i++) {
$arg[] = $this -> tempStr;
}
$result = vsprintf("%s", $arg);
}, $this -> reTryTime);
echo "time By vsprintf() is : ".$timeAvg."\n";
}
public function testJoin()
{
$timer = Benchmark::getInstance();
$timeAvg = $timer -> tryManyTimes(function () {
// alloc the array
$resultArr = [];
for ($i = 0;$i < $this -> Count;$i++) {
$resultArr[] = $this -> tempStr;
}
$result = implode('',$resultArr);
}, $this -> reTryTime);
echo "time by Join() / Implode() is : ".$timeAvg."\n";
}
}