PythonvsPHP bubble sorting and accumulative sum computing performance test

Source: Internet
Author: User
PythonvsPHP bubble sorting and accumulative sum computing performance test Test Environment: Processor i5-3230M, 64-bit Ubuntu 14.04
Python 2.7.6, PHP 5.4.39, PHP 7.0.0-dev (2015/04/21)

Test content:
Bubble sorting: sorts 10 ascending numbers, outputs in descending order, and loops 1 million times.
Cumulative Sum: 0 + 1 + 2 + 3 +... + 99999999

Comparison of bubble sorting test results:
Program: Python PHP5 PHP7
Time consumed: 16.910 s 14.715 s 8.011 s
Memory: 35.8 m 9.0 m 12.5 m

Comparison of accumulative sum test results:
Program: Python PHP5 PHP7
Time consumed: 10.057 s 3.855 s 1.855 s
Memory: 3.039 GB8.9 m 12.5 m
Python memory usage reaches 3 GB. what is the situation?

Conclusion:
Python and PHP are both dynamic scripting languages and do not have the JIT mechanism. Therefore, testing is fair.
The Python computing performance is inferior to that of PHP5, and the gap between PHP 7 and PHP 7 is even greater.

The detailed test process is as follows:

Python bubble sorting:
Def bubble_sort (lst ):
Length = len (lst)
For I in range (0, length, 1 ):
For j in range (0, length-1, 1 ):
If lst [j] <lst [j + 1]:
Temp = lst [j]
Lst [j] = lst [j + 1]
Lst [j + 1] = temp
Return lst
For c in range (0, 1000000 ):
Lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Lst = bubble_sort (lst)
Print (lst)
Test: time python BubbleSort. py
Time consumed: 0m16. 910 s
Memory: 35.8 MB



Python accumulative sum:
Sum = 0
For I in range (100000000 ):
Sum + = I
Print (sum)
Test: time python sum. py
Time consumed: 0m10. 057 s
Memory: 3.039 GB



PHP bubble sorting:
Function bubble_sort ($ array ){
$ Size = count ($ array );
For ($ I = 0; $ I <$ size; $ I ++ ){
For ($ j = 0; $ j <$ size-1; $ j ++ ){
If ($ array [$ j] <$ array [$ j + 1]) {
$ Temp = $ array [$ j];
$ Array [$ j] = $ array [$ j + 1];
$ Array [$ j + 1] = $ temp;
}
}
}
Return $ array;
}
For ($ c = 0; $ c <1000000; $ c ++ ){
$ Array = array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
$ Array = bubble_sort ($ array );
}
Print_r ($ array );
Test: time php BubbleSort. php
Time consumed: 0m14. 715 s
Memory: 9.0 MB
Test: time phpng BubbleSort. php
Time consumed: 0m8. 011 s
Memory: 12.5 MB




PHP accumulative sum:
$ Sum = 0;
For ($ I = 0; I I <100000000; $ I ++)
$ Sum + = $ I;
Print_r ($ sum );
Test: time php sum. php
Time consumed: 0m3. 855 s
Memory: 8.9 MB
Test: time phpng sum. php
Time consumed: 0m1. 855 s
Memory: 12.5 MB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.