Performance Experiment of C/Java/Python/objective-C on OS X

Source: Internet
Author: User

I saw an article about Python a few days ago.Article: How to Make Python faster -- fasterAlgorithmVariousProgramming LanguageWhat will happen when running on Mac OS X?

So I wrote the Fibonacci series implementation in four languages: C, Java, Python, and objective-C, and all adopt the least efficient and longest Recursive Implementation, without other data structures or formulas, it is easier to compare them. If the iteration method is used, the execution time is too short and difficult to compare.

In the first round of testing, No optimization is performed. In the second round, some compilation and environment optimization are performed respectively. Then, let's look at the results again.CodeAs follows:

C language, implement Recursive Computing Using Functions
 # include 
   
    long  fib (
    int  
    N) { 
    If  (n 
    2  
   )  
    return  
    N;  
    return  fib (n-
    1 ) + fib (n-
    2  
   ) ;} 
    int  
    main () {printf ( 
   "  
    fib = % LD  
   " , FIB (
    40  
   );  
    return  
    0  
    ;}  
  
Java, implements Recursive Computing Using static methods
 
Public ClassFIB {Public Static LongJfib (IntN ){If(N <2)ReturnN;ReturnJfib (n-1) + jfib (n-2);}Public Static VoidMain (string [] ARGs) {system. Out. println (jfib (40));}}
Python, implement Recursive Computing Using Functions
 
DefFIB (n ):IfN <2:ReturnNReturnFIB (n-1) + fib (n-2)PrintFIB (40)
Objective-C: Implement Recursive Computing Using Blocks
 # Import <Foundation/Foundation. h> Int Main ( Int Argc, Const   Char * Argv []) {@ autoreleasepool { Long (^__ Block Fib )( Long ) = ^ ( Long  Num ){  If (Num < 2  )  Return  Num;  Return FIB (Num- 1 ) + Fib (Num- 2  ) ;}; Nslog (  @" FIB: % LD  " , FIB (4 0  ));}  Return   0  ;} 
Basic test environment:

C language: i686-apple-darwin11-llvm-gcc-4.2

Java: Java version "1.6.0 _ 37", hotspot (TM) 64-bit

Python: Python 2.7.2 with GCC 4.2.1

Pypy: pypy 1.9.0 with GCC 4.2.1

Objective-C: 2.0 with llvm 4.1

 

Use the time command to calculate the execution time, such as time Python fib. py

The result of directly compiling and running is still surprising:

C: 1 second

Java: 0.63 seconds

Python: 45.79 seconds

Objective-C: 1.3 seconds

 

Result: Java> C> Objective-C> Python

This result makes people feel that Java is really not slow, and dynamic language is a little slow.

 

The second round of testing for C Program For python, use pypy to replace the native Python environment and set the optimization level to fastest for objective-C. The result is as follows: C: 0.35 seconds Java: 0.63 seconds Python: 4.96 seconds objective-C: 1.04 seconds result: C> JAVA> Objective-C> python the result tells us that C is still the fastest, pypy's optimization on python is still very obvious.

The above data is the testing result of performance proportional amplification on the OS X platform. in actual application, if the correct algorithm is used for different scenarios, the gap will not be so large, for example, we can use the iterative method to rewrite the python implementation, as shown below:
DefFIB (n ):IfN <2:ReturnN A1= A2 = a3 = 1WhileN> 2: N-= 1A3= A1 +A2 A1=A2=A3ReturnA3PrintFIB (40)
At this time, no matter whether Python compilation or pypy is used for execution, it takes about 0.02 seconds. There is no big difference. The execution result of the above Code is 102334155. If you are interested, try it on your own machine. Declaration: 1. The above code is for reference and entertainment only. In actual application, if you use the Fibonacci series, do not use recursive calling. iterative method should be a good choice. 2. Increasing the data volume may result in different results. If you are interested, try again. The experiment is complete and I hope you can refer to it.

 

Related Article

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.