php - PHP 中 str_replace 的性能

标签 php str-replace

这里我有 2 个方法使用 str_replace 来替换给定短语中的字符串。

// Method 1
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");
$phrase = str_replace($healthy, $yummy, $phrase);

// Method 2
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$phrase = str_replace("fruits", "pizza", $phrase);
$phrase = str_replace("vegetables", "beer", $phrase);
$phrase = str_replace("fiber", "ice cream", $phrase);

哪种方法更有效(在执行时间和使用的资源方面)?

假设真实的短语要长得多(例如 50,000 个字符),并且要替换的单词有更多的对。

我的想法是,方法2调用了3次str_replace,函数调用的开销会更大;另一方面,方法 1 创建 2 个数组,str_replace 需要在运行时解析 2 个数组。

最佳答案

我更愿意使用方法 1,因为它更清晰、更有条理,方法 1 也提供了使用来自其他来源的对的机会,例如:数据库中的坏词表。方法 2 需要另一个排序循环..

<?php
$time_start = microtime(true);
for($i=0;$i<=1000000;$i++){
    // Method 1
    $phrase  = "You should eat fruits, vegetables, and fiber every day.";
    $healthy = array("fruits", "vegetables", "fiber");
    $yummy   = array("pizza", "beer", "ice cream");
    $phrase = str_replace($healthy, $yummy, $phrase);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Test 1 in ($time seconds)\n<br />";



$time_start = microtime(true);
for($i=0;$i<=1000000;$i++){
    // Method2
    $phrase  = "You should eat fruits, vegetables, and fiber every day.";
    $phrase = str_replace("fruits", "pizza", $phrase);
    $phrase = str_replace("vegetables", "beer", $phrase);
    $phrase = str_replace("fiber", "ice cream", $phrase);

}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Test 2 in ($time seconds)\n";
?>  

在(3.6321988105774 秒)内完成测试 1

在(2.8234610557556 秒)内完成测试 2


编辑: 进一步测试字符串重复到 50k,更少的迭代和来自 ajreal 的建议,差异是如此微小。

<?php
$phrase  = str_repeat("You should eat fruits, vegetables, and fiber every day.",50000);
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$time_start = microtime(true);
for($i=0;$i<=10;$i++){
    // Method 1
    $phrase = str_replace($healthy, $yummy, $phrase);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Test 1 in ($time seconds)\n<br />";



$time_start = microtime(true);
for($i=0;$i<=10;$i++){
    // Method2
    $phrase = str_replace("fruits", "pizza", $phrase);
    $phrase = str_replace("vegetables", "beer", $phrase);
    $phrase = str_replace("fiber", "ice cream", $phrase);

}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Test 2 in ($time seconds)\n";
?>  

在(1.1450328826904 秒)内完成测试 1

在(1.3119208812714 秒)内完成测试 2

关于php - PHP 中 str_replace 的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8485511/

相关文章:

php - MySQL查询用PHP函数替换字段名

PHP 使用 str_replace 将数字更改为字符串而不混淆

PHP str_replace 用下划线替换空格

php - 从 HTTP 版本没有重定向或规范到 HTTPS 主页

php - PHP 操作码缓存是否与 __autoload 一起使用?

PHP 不向表中插入新数据

php - 如何使用 php 将字符串中的最后一个逗号替换为 "and"?

PHP,从数组读取时str_replace出现问题

php - 摆脱这个代理

php - 语法问题阻止 mySQL 结果显示在 HTML 表中