【发布时间】:2012-11-04 03:18:29
【问题描述】:
在我的本地服务器 XAMPP 环境中,我正在运行一个测试 PHP 脚本,它需要 20 秒才能运行,但只使用 2MB 内存和 10% 的 CPU。
当我打开一个新窗口并与第一个窗口同时运行相同的脚本时,两个脚本都需要 30 多秒才能完成。
--脚本是一个简单的for循环,写入mysql DB,InnoDb,200次。
脚本不应该花费相同的时间,但使用更多的系统资源吗?
如,线性缩放。
这是为什么?
//the code in all its glory-- Post extends a CRUD class
// These are the values to be saved:
$values = array(
'id' => '',
'content' => 'This is the VALUE'
);
//And the action. I know-Saving Mysql in a loop is a no-no--
//for demonstration only
for($i=0; $i<250; $i++){
$object = new Post($values); //instantiate the Post Class with values
$object->create($values); //save the values to the Db. The end
}
20 秒。
【问题讨论】:
-
innodb执行行级锁定。两个脚本是否都试图更新相同的行?两个脚本都在开始交易吗? -
200 行 20 秒?那里出了点问题
-
不,只是一个简单的插入,两列。
-
@Dagon 是的,这太慢了......不过,它应该线性增加时间,不是吗?
-
@Dagon 同意——我对 POG-phpobjectgenerator.com 做了同样的测试——在同一时间段内保存了大约 800 行。
标签: php performance concurrency