gpt4 book ai didi

PHP - 为什么比较两个完整的长(相同)字符串比比较每个字符串的第一个字符要快得多?

转载 作者:行者123 更新时间:2023-12-05 01:02:04 31 4
gpt4 key购买 nike

对于我的一个应用程序,我假设比较 2 个字符串的第一个字符比比较整个字符串是否相等要快。例如,如果我知道只有 2 个可能的字符串(在一组 n 字符串中)可以以相同的字母开头(比如说 'q'),如果是这样,它们是相同的字符串,那么我可能会写一个这样的比较:

if ($stringOne[0] === $stringTwo[0]) $qString = true;

代替:

if ($stringOne === $stringTwo) $qString = true;

但我最近编写了一些基准脚本,似乎我认为是错误的。也就是说,看起来第二次比较平均比第二次快 2-4 倍。我的基准是这样的:

$x = 'A really really looooooooooooong string';
$y = 'A really really looooooooooooong string';

$timeArray = array();

//Method 1, two-four times faster than Method 2
for($i = 0; $i < 100; $i++) {
$t1 = microtime(true);
for($j = 0; $j < 100000; $j++) {
if ($x === $y) continue;
}
$t2 = microtime(true);
$timeArray[] = $t2 - $t1;
}

echo array_sum($timeArray) / 100;//average time is echoed

//Method 2
for($i = 0; $i < 100; $i++) {
$t1 = microtime(true);
for($j = 0; $j < 100000; $j++) {
if ($x[0] === $y[0]) continue;
}
$t2 = microtime(true);
$timeArray[] = $t2 - $t1;
}

echo array_sum($timeArray) / 100;//average time is echoed

我想我假设由于每个字符串 $x 和 $y 都在内存中,那么每个字符串的第一个字符也在内存中,并且比较会更快。

为什么全字符串比较更快?从每个字符串中提取第一个字符进行比较是否有“成本”?

更新:即使在每个外部循环迭代中生成新字符串并进行比较,或者起始字符串是否相同,Method1 对我来说仍然比 Method2 快。

//Method 1 faster than Method 2 by 2-3 times
for($i = 0; $i < 100; $i++) {
$t1 = microtime(true);
$a = $x . $i;
$b = $y . $i;
for($j = 0; $j < 100000; $j++) {
if ($a === $b) continue;
}
$t2 = microtime(true);
$timeArray[] = $t2 - $t1;
}

//Method 2
for($i = 0; $i < 100; $i++) {
$t1 = microtime(true);
$a = $x . $i;
$b = $y . $i;
for($j = 0; $j < 100000; $j++) {
if ($a[0] === $b[0]) continue;
}
$t2 = microtime(true);
$timeArray[] = $t2 - $t1;
}

如果通过严格不等价而不是严格等价比较两者,也会得到相同的结果

//Method 1 faster than Method 2 by 1.5-2 times, but now less of a difference
for($i = 0; $i < 100; $i++) {
$t1 = microtime(true);
$a = $x . $i;
$b = $y . $i;
for($j = 0; $j < 100000; $j++) {
if ($a !== $b) continue; // using inequivalence this time
}
$t2 = microtime(true);
$timeArray[] = $t2 - $t1;
}

//Method 2
for($i = 0; $i < 100; $i++) {
$t1 = microtime(true);
$a = $x . $i;
$b = $y . $i;
for($j = 0; $j < 100000; $j++) {
if ($a[0] !== $b[0]) continue; // using inequivalence this time
}
$t2 = microtime(true);
$timeArray[] = $t2 - $t1;
}

最佳答案

静态字符串,如脚本中的 will be interned (参见 Wikipedia 上的 String Interning 了解其含义的详细说明)。

本质上,这意味着相同的字符串只会在内存中存储一​​次。当 PHP 进行比较时,它会立即看到两个字符串都引用了内存中的同一个对象,不需要做任何进一步的检查。字符串中单个字符的比较很可能不会从这种优化中受益,这就是它们可能需要更长时间的原因。

很可能还有其他因素在起作用,但这将是一个主要因素。尝试动态构造一个或两个字符串,并通过如下更改代码来查看结果有多大变化:

$x = base64_encode(base64_decode('A really really looooooooooooong string'));

正如评论中所 promise 的,这是一个脚本版本,它覆盖了字符串实习和可能正在使用的任何类型的相等缓存。

我在这里得到的结果表明第二种方法要快得多。

<?php
$runs = 1000000;
$input_string_a = "A really really looooooooooooong string";
$input_string_b = "B really really looooooooooooong string";

$total_time = 0;
for($i=0; $i<$runs; $i++) {
$a = substr($input_string_a, 0);
$b = substr($input_string_b, 0);
$start = microtime(true);
if($a === $b) {
if(false) break;
}
$end = microtime(true);
$total_time += $end - $start;
}

echo $total_time."\n";

$total_time = 0;
for($i=0; $i<$runs; $i++) {
$a = substr($input_string_a, 0);
$b = substr($input_string_b, 0);
$start = microtime(true);
if($a[0] === $b[0]) {
if(false) break;
}
$end = microtime(true);
$total_time += $end - $start;
}

echo $total_time."\n";

关于PHP - 为什么比较两个完整的长(相同)字符串比比较每个字符串的第一个字符要快得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262960/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com