dongyu9263 2011-05-10 01:19
浏览 82
已采纳

性能明智...... exec(c ++)还是直接的PHP?

Wondering what would be quicker for a mysql open, select, and a generic output of the data:

A) C++ code compiled, an being called through exec() (or something equivalent)

or

B) Straight PHP Code.

Given that all code is equally coded in both C++ and PHP.

Did a test: this is the 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

This is the 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

Here are the programs (I did not include MySql code -just wanted to see if the stack creation was quicker with basic code).

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;
}

Here is the PHP:

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

echo $x;

Don't know if its a fair test using stringsteam... thoughts? My thought is, C++ is going to be faster regardless of the new stack instance, and mysql connections.

Edit:

Tested with this:

#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;
}

and, still quicker!

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

Edit:

Here is the php program that calls the C++ file

<?php

echo exec("./test");
  • 写回答

3条回答 默认 最新

  • douxiza9868 2011-05-10 01:25
    关注

    So let's see. Let's fire up a shell, pass it some arguments, which fires up another app, which loads a series of .so libraries, and THEN connects to mysql, does stuff, then has to return a potentially huge lump of output via printf() or equivalent, which then has to be fed back to PHP, parsed as plain text, torn apart, and then turned into some kind of coherent structure.

    And this would be faster than doing the query within MySQL and getting the data directly into a PHP structure?

    Maybe you should test it...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作