Comparing the speed of C and PHP, we always know that PHP is written in C. most of the modules that consume performance on the Internet are rewritten by PHP's extended C, I have read Baidu's article about PHP performance & nbsp; on the internet. I agree with this article. It was a long time ago when I compared PHP and C, but I was not familiar with ZendAPI. now I am going to test the gap between C and PHP in this article. Comparison of C and PHP speed
I have always known that PHP is written in C. most of the modules that consume performance on the Internet are rewritten by PHP's extended C. I have seen Baidu's big talk about PHP performance on the Internet, I agree with this article.
It was a long time ago when I compared PHP and C, but I was not familiar with Zend API. now I am going to test the gap between C and PHP in this article.
I don't know what to test, so the question is to sort an array of 3000 in size by bubble and check the time.
There are three cases
- C implementation
- PHP implementation
- Php c extension, which generates arrays by PHP and then submits them to C for data processing.
1. C implementation
# Include
# Include
# Include "time. h "int main (int arg, char ** argv) {clock_t start_time = clock (); int data [3000]; int I; int length = sizeof (data) /sizeof (int); for (I = 0; I
Data [j + 1]) {temp = data [j]; data [j] = data [j + 1]; data [j + 1] = temp ;}}} /* for (I = 0; I
2. PHP
$data[$j+1]) { $temp = $data[$j]; $data[$j] = $data[$j+1]; $data[$j+1] = $temp; } } }// var_dump($data); $end_time =microtime(true); $time = $end_time-$start_time; echo "use time:" ,$time;
3. php c extension, which generates arrays by PHP and then submits them to C for data processing.
PHP_FUNCTION (bubble) {zval * array; zval ** item; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "a", & array) = FAILURE) {return ;} int I; int count; count = zend_hash_num_elements (Z_ARRVAL_P (array); int j, temp; int data [count]; zend_hash_internal_pointer_reset (Z_ARRVAL_P (array )); for (I = 0; I
Data [j + 1]) {temp = data [j]; data [j] = data [j + 1]; data [j + 1] = temp ;}}} for (I = 0; I
And then call
Tested
# Differences between C and PHP speed
Test method to sort an array with a size of 3000
1. C
Use time: 0.090000-0.110000
2. PHP
Use time: 11.867825984955
3. PHP expansion (C)
Use time: 0.12569403648376
We can clearly see that C is more than 100 times faster than PHP in this case. PHP's expansion will convert the module into C for processing, which can also reduce the gap.
On the one hand, this gap must be a weak variable of different types in PHP and C (PHP variables are represented by zval in the kernel, which abstracts seven variables: long, float, obj, array, etc.), there are exaggerated function encapsulation (visual testing of this is very good performance), resulting in a low speed.
However, this example may not be representative, but we can also understand it.
From this we can know why so many PHP functions are written in C. Most of PHP's external functions are written in C, and pear and pecl came into being, no wonder a PHP programmer who does not write C is not really a good programmer.
The Yaf of laruence in the Chinese PHP field is written in C, and the legendary fastest PHP Web framework. I really want to study the source code if I have the opportunity.
From independent blog http://blog.imsuzie.com/archives/573