测试更新为更具可读性;所有这些都在 100 倍的 foreach 循环中完成。
测试查询是SELECT * FROM school_courses;
任何人都可以提供“跳出框框思考”的反馈:
a) 为什么 PHP ActiveRecord ORM 需要 4 秒才能根据以下结果执行相同的查询?
b) 这是比较查询方法的实际基准还是更多的假设基准?
c)是否有其他方法(测试用例)我应该尝试(或修改这些方法)以获得更清晰的画面?
结果(使用 PDO 和 MySQLi)
Iterations: 100
PHP (config file)
Base Time: 5.793571472168E-5
Gross Time: 0.055607080459595
Net Time: 0.055549144744873
PHP ActiveRecord ORM
Base Time: 5.2213668823242E-5
Gross Time: 4.1013090610504
Net Time: 4.1012568473816
MySQL (standard)
Base Time: 5.1975250244141E-5
Gross Time: 0.32771301269531
Net Time: 0.32766103744507
CodeIgniter (Active Record)
Base Time: 5.1975250244141E-5
Gross Time: 0.28282189369202
Net Time: 0.28276991844177
MySQLi
Base Time: 5.1975250244141E-5
Gross Time: 0.20240592956543
Net Time: 0.20235395431519
PDO
Base Time: 5.2928924560547E-5
Gross Time: 0.17662906646729
Net Time: 0.17657613754272
测试
// Benchmark tests
$runs = 100;
// PHP (config file)
for ($i = 0; $i < $runs; $i++) {
$this->view_data['courses'] = course_info();
}
// PHP ActiveRecord ORM
for ($i = 0; $i < $runs; $i++) {
$this->view_data['courses'] = Course::all();
}
// mysql_* (MySQL standard; deprecated)
for ($i = 0; $i < $runs; $i++) {
$sql = mysql_query('SELECT * FROM school_courses') or die(mysql_error());
while ($row = mysql_fetch_object($sql)) {
array_push($this->view_data['courses'], $row);
}
}
// CodeIgniter (Active Record)
for ($i = 0; $i < $runs; $i++) {
$this->view_data['courses'] = $this->db->get('school_courses');
}
// mysqli_* (MySQLi)
for ($i = 0; $i < $runs; $i++) {
$res = $mysqli->query('SELECT * FROM school_courses');
while ($row = $res->fetch_object()) {
array_push($this->view_data['courses'], $row);
}
}
// PDO
for ($i = 0; $i < $runs; $i++) {
foreach($conn->query('SELECT * FROM school_courses') as $row) {
array_push($this->view_data['courses'], $row);
}
}