我有10个文件,每个文件包含50到250行.
我需要能够从不同的文件中提取一个或多个随机行.
我目前这样做……
$lines = file($filePath);
if ($number == "1") {
return $lines[array_rand($lines)];
}
else {
shuffle($lines);
return array_slice($lines, 0, $number);
}
但是,我刚刚在这里阅读了使用MySQL做同样事情的方法:
将这10个文件移动到MySQL数据库并执行该查询时,我真的会获得如此大的性能提升吗?
谢谢!
解决方法:
根据我的经验,当您没有太多信息时,从文件中检索信息比从数据库检索信息更快.在我的情况下,我有文件,我存储数组的房子类型,例如,从文件中获取它更快.这些文件只有大约10kb,而且性能提高了10倍,也许我的性能提升有问题,但定义上它足够快,让我删除那些包含那些行的表并使用文本文件xD.
确保性能的最佳方法是使用函数microtime()并以两种方式完成.然后,您可以通过自己的基准测试看到性能.
我经常使用它:
$start = microtime(true);
// my code;
// If it's not a very long script, you should always put it inside a loop, let's say thousands times or more, depending on the script, because sometimes just the resources used by the system could vary the benchmark, as suggested by @Colin Morelli .
echo microtime(true) - $start;
您获得经过的微秒的输出.
标签:php,mysql,performance
来源: https://codeday.me/bug/20190709/1410731.html