我最近用PHPCPP创建了一个php扩展 - C++用于开发PHP扩展的库,并期望性能提升,但是我没有看到提升,我只看到性能下降。我相信我做错了什么,也许有人可能会发现为什么会这样?
这是一个C++代码:
#include <phpcpp.h>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <regex>
#include <stdlib.h> /* atof,strtod */
#include <cstddef> // std::size_t
#include <sstream>
#include <vector>
#include <boost/algorithm/string.hpp> // include Boost, a C++ library
#include <boost/regex.hpp>
using namespace std;
Php::Value test_function(Php::Parameters ¶ms)
{
double a = params[0];
double b = params[1];
return (a + b) * (a - b) / 100;
}
/**
* Switch to C context, because the Zend engine expects get get_module()
* to have a C style function signature
*/
extern "C" {
/**
* Startup function that is automatically called by the Zend engine
* when PHP starts, and that should return the extension details
* @return void*
*/
PHPCPP_EXPORT void *get_module()
{
// the extension object
static Php::Extension extension("test_extension", "1.0");
// add functions so that they can be called from PHP scripts
extension.add("test_function", test_function, {
Php::ByVal("a", Php::Type::Float,true),
Php::ByVal("b", Php::Type::Float,true)
});
// return the extension details
return extension;
}
}
下面是命令行扩展编译:
[root@test test_extension]# make
g++ -Wall -c -O2 -std=c++11 -fpic -o main.o main.cpp
g++ -shared -o test_extension.so main.o -lphpcpp
[root@test test_extension]# make install
cp -f test_extension.so /usr/lib64/php/modules
cp -f test_extension.ini /etc/php.d
这是我的简单测试:
<?php
//function written in php
function local_test_function($a,$b){
$res = ($a + $b) * ($a - $b) / 100;
return $res;
}
$output = '';
//local php function test
$start_time = microtime(true);
$output.= "<br> test_function local: " . local_test_function(123.4,12.5);
$end_time = microtime(true);
$duration = $end_time - $start_time;
$duration = number_format($duration,20);
$output.=", Duration: ".$duration.'<br>';
// function written in c++
$start_time = microtime(true);
$output.= "function written in c++: " . test_function(123.4,12.5);
$end_time = microtime(true);
$duration = $end_time - $start_time;
$duration = number_format($duration,20);
$output.=", Duration: ".$duration . '<br>';
?>
结果是:
test_function local result: 150.7131, Duration: 0.00000715255737304688
function written in c++ result: 150.7131, Duration: 0.00003004074096679688
结论 用 C++ 编写的 PHP 函数慢了 4 倍。 有人能说出为什么吗?有没有办法在C++中改进我的代码?
更新和答案
正如建议的那样,我正在尝试使用更复杂的函数进行测试,以查看开销是否导致C++函数变慢
我已经修改了现有的"test_function"+添加了另一个函数"test_function_inner"并声明了它。
double test_function_inner(double a, double b);
Php::Value test_function(Php::Parameters ¶ms)
{
double a = params[0];
double b = params[1];
double res = 0.0;
int i = 100;
while (i > 0) {
res = res + test_function_inner(a, b);
--i;
}
return res;
}
double test_function_inner(double a, double b) {
double res = 0.0;
while (b > a) {
res = res + (a + b) * (a - b) / 100;
b = b - 1;
}
return res;
}
测试:
<?php
$output = '';
//local php function test
$start_time = microtime(true);
$output.= "<br> test_function local: " . local_test_function(123.4,1200000.5);
$end_time = microtime(true);
$duration = $end_time - $start_time;
$duration = number_format($duration,20);
$output.=", Duration: ".$duration.'<br>';
// function written in c++
$start_time = microtime(true);
$output.= "function written in c++: " . test_function(123.4,1200000.5);
$end_time = microtime(true);
$duration = $end_time - $start_time;
$duration = number_format($duration,20);
$output.=", Duration: ".$duration . '<br>';
?>
结果:
test_function local result: -5.7600142172989E+17, Duration: 9.23940896987915039062
function written in c++ result: -5.7600142172989E+17, Duration: 0.72759604454040527344
结论这是由建议的初始函数调用引起的开销,并且小的操作和计算不值得放入扩展中的单独函数中,因为它不会提升应用程序,而是会对性能造成负面影响。