给定索引(PHP),在字符串中获取字符的最快方法

时间:2022-09-26 23:31:00

I know of several ways to get a character off a string given the index.

我知道有几种方法可以从给定索引的字符串中提取字符。

<?php
$string = 'abcd';
echo $string[2];
echo $string{2};
echo substr($string, 2, 1);
?>

I don't know if there are any more ways, if you know of any please don't hesitate to add it. The question is, if I were to choose and repeat a method above a couple of million times, possibly using mt_rand to get the index value, which method would be the most efficient in terms of least memory consumption and fastest speed?

我不知道是否还有其他的方法,如果你知道,请不要犹豫,添加它。问题是,如果我选择并重复使用超过几百万次的方法,可能使用mt_rand来获得索引值,那么哪种方法在内存消耗最少和速度最快的情况下是最有效的?

1 个解决方案

#1


18  

To arrive at an answer, you'll need to setup a benchmark test rig. Compare all methods over several (hundreds of thousands or millions) iterations on an idle box. Try the built-in microtime function to measure the difference between start and finish. That's your elapsed time.

要得到答案,您需要设置一个基准测试平台。比较空闲框上的多个(数十万或数百万)迭代中的所有方法。尝试内置的微时间函数来测量开始和结束之间的区别。那是你的时间。

The test should take you all of 2 minutes to write.

考试需要你花2分钟的时间来写。

To save you some effort, I wrote a test. My own test shows that the functional solution (substr) is MUCH slower (expected). The idiomatic PHP ({}) solution is as fast as the index method. They are interchangeable. The ([]) is preferred, as this is the direction where PHP is going regarding string offsets.

为了节省一些精力,我写了一个测试。我自己的测试表明,函数解决方案(substr)要慢得多(预期)。惯用的PHP({})解决方案与索引方法一样快。它们是可以互换的。([])是首选,因为这是PHP处理字符串偏移量的方向。

<?php
$string = 'abcd';
$limit = 1000000;

$r = array(); // results

// PHP idiomatic string index method
$s = microtime(true);
for ($i = 0; $i < $limit; ++$i) {
    $c = $string{2};
}
$r[] = microtime(true) - $s; 
echo "\n";

// PHP functional solution
$s = microtime(true);
for ($i = 0; $i < $limit; ++$i) {
    $c = substr($string, 2, 1); 
}
$r[] = microtime(true) - $s; 
echo "\n";

// index method
$s = microtime(true);
for ($i = 0; $i < $limit; ++$i) {
    $c = $string[2];
}
$r[] = microtime(true) - $s; 
echo "\n";


// RESULTS
foreach ($r as $i => $v) {
    echo "RESULT ($i): $v \n";
}
?>

Results:
RESULT (PHP4 & 5 idiomatic braces syntax): 0.19106006622314
RESULT (string slice function): 0.50699090957642
RESULT (*index syntax, the future as the braces are being deprecated *): 0.19102001190186

结果:结果(PHP4 & 5惯用大括号语法):0.19106006622314结果(string slice函数):0.50699090957642结果(*索引语法,作为大括号的未来*):0.19102001190186

#1


18  

To arrive at an answer, you'll need to setup a benchmark test rig. Compare all methods over several (hundreds of thousands or millions) iterations on an idle box. Try the built-in microtime function to measure the difference between start and finish. That's your elapsed time.

要得到答案,您需要设置一个基准测试平台。比较空闲框上的多个(数十万或数百万)迭代中的所有方法。尝试内置的微时间函数来测量开始和结束之间的区别。那是你的时间。

The test should take you all of 2 minutes to write.

考试需要你花2分钟的时间来写。

To save you some effort, I wrote a test. My own test shows that the functional solution (substr) is MUCH slower (expected). The idiomatic PHP ({}) solution is as fast as the index method. They are interchangeable. The ([]) is preferred, as this is the direction where PHP is going regarding string offsets.

为了节省一些精力,我写了一个测试。我自己的测试表明,函数解决方案(substr)要慢得多(预期)。惯用的PHP({})解决方案与索引方法一样快。它们是可以互换的。([])是首选,因为这是PHP处理字符串偏移量的方向。

<?php
$string = 'abcd';
$limit = 1000000;

$r = array(); // results

// PHP idiomatic string index method
$s = microtime(true);
for ($i = 0; $i < $limit; ++$i) {
    $c = $string{2};
}
$r[] = microtime(true) - $s; 
echo "\n";

// PHP functional solution
$s = microtime(true);
for ($i = 0; $i < $limit; ++$i) {
    $c = substr($string, 2, 1); 
}
$r[] = microtime(true) - $s; 
echo "\n";

// index method
$s = microtime(true);
for ($i = 0; $i < $limit; ++$i) {
    $c = $string[2];
}
$r[] = microtime(true) - $s; 
echo "\n";


// RESULTS
foreach ($r as $i => $v) {
    echo "RESULT ($i): $v \n";
}
?>

Results:
RESULT (PHP4 & 5 idiomatic braces syntax): 0.19106006622314
RESULT (string slice function): 0.50699090957642
RESULT (*index syntax, the future as the braces are being deprecated *): 0.19102001190186

结果:结果(PHP4 & 5惯用大括号语法):0.19106006622314结果(string slice函数):0.50699090957642结果(*索引语法,作为大括号的未来*):0.19102001190186