Kladskull
Kladskull

Reputation: 10762

Performance wise... exec(c++) or straight 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");

Upvotes: 2

Views: 813

Answers (3)

mario
mario

Reputation: 145512

If you patch your compliled C++ code in as PHP extension, then yes it would make sense. But unless you are doing significant calculations on the result data with C/C++ it's not faster than executing it as PHP scripting code.

You are more likely to see the performance increase if you optimize/denormalize your database scheme instead, or use the query asynchronously (issue mysql_query at start of script, do something else, and only mysql_fetch_assoc when the mysqld server had a few milliseconds to prepare the result sets).

Upvotes: 0

Marc B
Marc B

Reputation: 360872

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...

Upvotes: 5

eykanal
eykanal

Reputation: 27077

Unless you're handling hundreds of read/writes per second, the difference will very likely be be close to negligible, if extant at all.

Upvotes: 0

Related Questions