PHP与MySQL获取随机 – 查询与从文件读取 – 性能
发布时间:2020-12-13 22:48:03 所属栏目:PHP教程 来源:网络整理
导读:我有10个文件,每个文件包含50到250行. 我需要能够从不同的文件中提取一个或多个随机行. 我目前这样做…… $lines = file($filePath);if ($number == "1") { return $lines[array_rand($lines)];}else { shuffle($lines); return array_slice($lines,$number)
我有10个文件,每个文件包含50到250行.
我需要能够从不同的文件中提取一个或多个随机行. 我目前这样做…… $lines = file($filePath); if ($number == "1") { return $lines[array_rand($lines)]; } else { shuffle($lines); return array_slice($lines,$number); } 但是,我刚刚在这里阅读了使用MySQL做同样事情的方法: https://stackoverflow.com/a/4329447/390480 将这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; 您获得经过的微秒的输出. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |