性能方面..exec(c ++)或直接PHP

Performance wise... exec(c++) or straight PHP?

本文关键字:PHP 方面 exec 性能      更新时间:2023-10-16

想知道对于 mysql 打开、选择和数据的一般输出来说,什么会更快:

A) 编译C++代码,通过 exec()(或等效的东西)调用

B)直接的PHP代码。

鉴于所有代码在 C++ 和 PHP 中编码相同。

做了一个测试:这是C++

Document Length:        100000 bytes
Concurrency Level:      2
Time taken for tests:   0.139 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      1001550 bytes
HTML transferred:       1000000 bytes
Requests per second:    71.76 [#/sec] (mean)
Time per request:       27.872 [ms] (mean)
Time per request:       13.936 [ms] (mean, across all concurrent requests)
Transfer rate:          7018.29 [Kbytes/sec] received

这是 PHP:

Concurrency Level:      2
Time taken for tests:   4.115 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      1001550 bytes
HTML transferred:       1000000 bytes
Requests per second:    2.43 [#/sec] (mean)
Time per request:       822.924 [ms] (mean)
Time per request:       411.462 [ms] (mean, across all concurrent requests)
Transfer rate:          237.71 [Kbytes/sec] received

以下是程序(我没有包含MySql代码 - 只是想看看使用基本代码创建堆栈是否更快)。

C++

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
    stringstream x;
    //string x;
    for (int i = 0; i < 100000; ++i)
    {
        x << "X";
    }
    cout << x.str();
    return 0;
}

这是 PHP:

for ($i=0; $i<100000; ++$i)
{
    $x = $x . "X";
}
echo $x;

不知道使用字符串蒸汽是否是公平的测试... 思潮? 我的想法是,无论新的堆栈实例和mysql连接如何,C++都会更快。

编辑:

用这个测试:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
    //stringstream x;
    string x;
    for (int i = 0; i < 100000; ++i)
    {
        x = x + "X";
    }
    cout << x;
    return 0;
}

而且,速度更快!

Concurrency Level:      2
Time taken for tests:   0.115 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      1740 bytes
HTML transferred:       0 bytes
Requests per second:    86.98 [#/sec] (mean)
Time per request:       22.994 [ms] (mean)
Time per request:       11.497 [ms] (mean, across all concurrent requests)
Transfer rate:          14.78 [Kbytes/sec] received

编辑:

这是调用C++文件的 php 程序

<?php
echo exec("./test");

让我们看看。让我们启动一个 shell,传递给它一些参数,它会启动另一个应用程序,该应用程序加载一系列 .so 库,然后连接到 mysql,做一些事情,然后必须通过 printf() 或等效物返回潜在的大量输出,然后必须将其反馈给 PHP,解析为纯文本,撕裂, 然后变成了某种连贯的结构。

这比在MySQL中执行查询并将数据直接获取到PHP结构中更快吗?

也许你应该测试一下...

除非您每秒处理数百次读/写,否则差异很可能几乎可以忽略不计,如果有的话。

如果您将

编译的C++代码作为 PHP 扩展进行修补,那么是的,这将是有意义的。但是,除非您使用 C/C++ 对结果数据进行大量计算,否则它不会比将其作为 PHP 脚本代码执行更快。

如果您优化/非规范化数据库方案,或者异步使用查询(在脚本开始时发出mysql_query,执行其他操作,并且仅在 mysqld 服务器有几毫秒的时间来准备结果集时才mysql_fetch_assoc,则更有可能看到性能提升。