gpt4 book ai didi

php - 我可以加快速度吗?

转载 作者:行者123 更新时间:2023-11-29 00:27:37 25 4
gpt4 key购买 nike

我担心速度,因为 do 循环可能必须运行 15-20 次。在每个循环中,我搜索和替换几次,每次从 mysql 表中选择一次。我只想让它尽可能快。

$text = file_get_contents('texts/phir-mohabbat.txt');
preg_match_all('/\S+@/', $text, $words);
preg_match_all('/@\S+/', $text, $lexs);

$count = count($words[0]);
$i = 0;
do {
$bad = array('@', ',', '।', '?');
$word = str_replace('@', '', $words[0][$i]);
$lex = str_replace($bad, '', $lexs[0][$i]);
if($lex == '#') {$lex = $word;}

$get_def = mysqli_query($con,"SELECT * FROM hindi_dictionary WHERE lex = '$lex'");
while($row = mysqli_fetch_array($get_def)) {
$def = $row['def'];
}
$find = array($word.'@'.$lex, $word.'@#');
$replace = '<span class = "word-info" onmouseover="show_info(\''.$lex.' - '.$def.'\',\''.$word.'\');">'.$word.'</span>';

$text = str_replace($find, $replace, $text);
$i++;
} while ($i < $count);

echo nl2br($text);

最佳答案

我看到@Blorgbeard 比我快:)您可以更改您的代码以仅调用一次数据库 - 这应该会显着提高速度,例如:

...
$res = array();
for($i=0; $i<$count; $i++){
$word = str_replace('@', '', $words[0][$i]);
$lex = str_replace($bad, '', $lexs[0][$i]);
if($lex == '#') {$lex = $word;}
$res[] = $lex;
}
$query = "SELECT * FROM hindi_dictionary WHERE lex in ('";
$query .= implode($res,"','") . "')";

echo $query; // something like: "SELECT * FROM hindi_dictionary WHERE lex in ('a','b','c','f')"
...

并继续按照您最初的预期处理返回的结果集 - 只是现在您将一次性收到所有结果并对其进行迭代。

评论:

不建议使用 mysql_* 函数,这些函数已被弃用且易受 sql 注入(inject)攻击(参见 red box)。更好用PDOMySQLi .

关于php - 我可以加快速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222818/

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