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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!