性能方面..exec(c ++)或直接PHP
Performance wise... exec(c++) or straight PHP?
想知道对于 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,则更有可能看到性能提升。
相关文章:
- GCC和Clang在与__builtin_constant_p相关的static_assert方面有所不同
- 将加密消息从 php 发送到 C++ 应用程序,然后使用 CryptoPP 进行解密
- 为什么我的代码在指针方面停止运行?
- 你如何使std::变体的使用更"palatable",语法方面?
- 我需要 c++ 结构方面的帮助
- 我需要 c++ 结构方面的帮助
- PHP Profiler语言 - 使用 C++ 从 PHP 扩展获取客户端 IP 和 URL
- Nginx RTMP与InstagramLive-PHP不起作用
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?
- 为什么酷睿i5-6600在非方阵乘法方面比酷睿i9-9960X更快?
- printf() 和 std::cout 在指针方面的区别
- Boost.Python 在静态库方面失败
- 为什么python在循环方面胜过c++
- 在设计方面:重载vector类型的类成员的插入运算符
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- 编译方面的问题.错误E0413、E0434、C2664、C2440
- PHP & C++ $_FILES 和 HttpSendRequestA
- 如何使用 PHP-CPP 在 PHP 类中添加静态数组
- 如何在PHPCPP库中将C++map<string,map<string>>转换为php::value?
- 性能方面..exec(c ++)或直接PHP