2

我最近使用PHPCPP 创建了一个 php 扩展 - 用于开发 PHP 扩展的 C++ 库,并期待性能提升,但是我只看到性能下降,而不是看到提升。我相信我做错了什么,也许有人可能会发现为什么会这样?

这是一个 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 &params)
{
    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++ 代码?

4

1 回答 1

1

更新和回答

正如建议的那样,我正在尝试使用更复杂的函数进行测试,以查看开销是否导致 C++ 函数变慢

我已经修改了现有的“test_function”+ 添加了另一个函数“test_function_inner”并声明了它。

double test_function_inner(double a, double b);

      Php::Value test_function(Php::Parameters &params)
    {
        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

结论 这是建议的初始函数调用引起的开销,并且小操作和计算不值得在扩展中放入单独的函数,因为它不会提升应用程序,而是会对性能产生负面影响。

于 2017-08-10T08:42:41.073 回答