更快的方法是什么?
1。加入数组?
1.1 加入数组(未设置)
下面的代码:
for($i=1;$i<1000;$i++){
$string[]='lorem_';
$string[]= $i;
$string[]='_ipsum';
$string = join('',$string);
var_dump($string);
unset($string);
}
产生这个平均性能:
.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
| 5.6 | 0.016 | 0.061 | 20.60 |
| 7.0 | 0.009 | 0.010 | 16.69 |
| 7.1 | 0.011 | 0.011 | 24.18 |
| 7.2 | 0.003 | 0.017 | 19.41 |
'---------'----------------'--------------'--------------'
来源:https://3v4l.org/8YK1H/perf#output
1.2 加入数组(未设置)
下面的代码:
for($i=1;$i<1000;$i++){
$string = [];
$string[]='lorem_';
$string[]= $i;
$string[]='_ipsum';
$string = join('',$string);
var_dump($string);
// unset($string);
}
产生这个平均性能:
.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
| 5.6 | 0.017 | 0.068 | 20.58 |
| 7.0 | 0.012 | 0.013 | 16.13 |
| 7.1 | 0.021 | 0.012 | 23.23 |
| 7.2 | 0.013 | 0.010 | 17.65 |
'---------'----------------'--------------'--------------'
来源:https://3v4l.org/kU4QG/perf#output
2。速记运算符?
2.1 速记运算符(未设置)
下面的代码:
for($i=1;$i<1000;$i++){
$string = 'lorem_';
$string .= $i;
$string .= '_ipsum';
// $string = join('',$string);
var_dump($string);
unset($string);
}
产生这个平均性能:
.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
| 5.6 | 0.012 | 0.059 | 20.60 |
| 7.0 | 0.008 | 0.011 | 16.65 |
| 7.1 | 0.014 | 0.012 | 24.22 |
| 7.2 | 0.012 | 0.009 | 19.08 |
'---------'----------------'--------------'--------------'
来源:https://3v4l.org/cCoSC/perf#output
2.2 速记运算符(未设置)
下面的代码:
for($i=1;$i<1000;$i++){
$string = 'lorem_';
$string .= $i;
$string .= '_ipsum';
// $string = join('',$string);
var_dump($string);
// unset($string);
}
产生这个平均性能:
.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
| 5.6 | 0.011 | 0.059 | 20.60 |
| 7.0 | 0.008 | 0.011 | 16.70 |
| 7.1 | 0.014 | 0.011 | 24.22 |
| 7.2 | 0.010 | 0.010 | 19.33 |
'---------'----------------'--------------'--------------'
来源:https://3v4l.org/iKnaV/perf#output
3。单行连接?
3.1 单行连接(未设置)
下面的代码:
for($i=1;$i<1000;$i++){
$string = 'lorem_' . $i . '_ipsum';
// $string .= $i;
// $string .= '_ipsum';
// $string = join('',$string);
var_dump($string);
unset($string);
}
产生这个平均性能:
.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
| 5.6 | 0.015 | 0.066 | 20.60 |
| 7.0 | 0.008 | 0.012 | 16.68 |
| 7.1 | 0.014 | 0.014 | 24.23 |
| 7.2 | 0.010 | 0.014 | 19.45 |
'---------'----------------'--------------'--------------'
来源:https://3v4l.org/tv4X0/perf#output
3.2 单行连接(不设置)
下面的代码:
for($i=1;$i<1000;$i++){
$string = 'lorem_' . $i . '_ipsum';
// $string .= $i;
// $string .= '_ipsum';
// $string = join('',$string);
var_dump($string);
// unset($string);
}
产生这个平均性能:
.---------.----------------.--------------.--------------.
| Version | System time(s) | User time(s) | Memory (MiB) |
|---------|----------------|--------------|--------------|
| 5.6 | 0.012 | 0.056 | 20.65 |
| 7.0 | 0.008 | 0.008 | 16.69 |
| 7.1 | 0.014 | 0.010 | 24.17 |
| 7.2 | 0.007 | 0.011 | 19.39 |
'---------'----------------'--------------'--------------'
来源:https://3v4l.org/mF0YW/perf#output
如果按 PHP 版本合并所有结果:
PHP 5.6
.-------------.----------------.--------------.--------------.
| Code | System time(s) | User time(s) | Memory (MiB) |
|-------------|----------------|--------------|--------------|
| 1.1 | 0.016 | 0.061 | 20.60 |
| 1.2 | 0.017 | 0.068 | >> 20.58 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 1 | 0.0165 | 0.0645 | >> 20.59 |
|-------------|----------------|--------------|--------------|
| 2.1 | 0.012 | 0.059 | 20.60 |
| 2.2 | >> 0.011 | 0.059 | 20.60 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 2 | >> 0.0115 | >> 0.059 | 20.60 |
|-------------|----------------|--------------|--------------|
| 3.1 | 0.015 | 0.066 | 20.60 |
| 3.2 | 0.012 | >> 0.056 | 20.65 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 3 | 0.0135 | 0.061 | 20.625 |
'-------------'----------------'--------------'--------------'
PHP 7.0
.-------------.----------------.--------------.--------------.
| Code | System time(s) | User time(s) | Memory (MiB) |
|-------------|----------------|--------------|--------------|
| 1.1 | 0.009 | 0.010 | 16.69 |
| 1.2 | 0.012 | 0.013 | >> 16.13 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 1 | 0.0105 | 0.0115 | >> 16.41 |
|-------------|----------------|--------------|--------------|
| 2.1 | >> 0.008 | 0.011 | 16.65 |
| 2.2 | >> 0.008 | 0.011 | 16.70 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 2 | >> 0.008 | 0.011 | 16.675 |
|-------------|----------------|--------------|--------------|
| 3.1 | >> 0.008 | 0.012 | 16.68 |
| 3.2 | >> 0.008 | >> 0.008 | 16.69 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 3 | >> 0.008 | >> 0.010 | 16.685 |
'-------------'----------------'--------------'--------------'
PHP 7.1
.-------------.----------------.--------------.--------------.
| Code | System time(s) | User time(s) | Memory (MiB) |
|-------------|----------------|--------------|--------------|
| 1.1 | >> 0.011 | 0.011 | 24.18 |
| 1.2 | 0.021 | 0.012 | >> 23.23 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 1 | 0.016 | >> 0.0115 | >> 23.705 |
|-------------|----------------|--------------|--------------|
| 2.1 | 0.014 | 0.012 | 24.22 |
| 2.2 | 0.014 | 0.011 | 24.22 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 2 | >> 0.014 | >> 0.0115 | 24.22 |
|-------------|----------------|--------------|--------------|
| 3.1 | 0.014 | 0.014 | 24.23 |
| 3.2 | 0.014 | >> 0.010 | 24.17 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 3 | >> 0.014 | 0.012 | 24.20 |
'-------------'----------------'--------------'--------------'
PHP 7.2
.-------------.----------------.--------------.--------------.
| Code | System time(s) | User time(s) | Memory (MiB) |
|-------------|----------------|--------------|--------------|
| 1.1 | >> 0.003 | 0.017 | 19.41 |
| 1.2 | 0.013 | 0.010 | >> 17.65 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 1 | >> 0.008 | 0.0135 | >> 18.53 |
|-------------|----------------|--------------|--------------|
| 2.1 | 0.012 | >> 0.009 | 19.08 |
| 2.2 | 0.010 | 0.010 | 19.33 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 2 | 0.011 | >> 0.0905 | 19.205 |
|-------------|----------------|--------------|--------------|
| 3.1 | 0.010 | 0.014 | 19.45 |
| 3.2 | 0.007 | 0.011 | 19.39 |
| |-- -- -- -- -- -|-- -- -- -- --|-- -- -- -- --|
| (average) 3 | 0.0085 | 0.0125 | 19.42 |
'-------------'----------------'--------------'--------------'
结论
对于系统时间,更快的方式(0.003秒)是在PHP 7.2.0中使用join arrays(with unset)强>.
对于用户时间,更快的方式(0.003秒)是使用oneline concatenation without unset in PHP 7.0.22 .
对于内存使用,“最经济”(14.47 MiB)的方式是使用join array without unset in PHP 7.0.14强>.