Introduction to Python functions that are more efficient than C

Source: Internet
Author: User

C language and Python running efficiency which of the following do you prefer in actual application? This article describes how to compare the running efficiency of C language and Python, highlight the differences between the beginning and end, and the related functions in the relevant application process. The following is an introduction.

Python is a very popular interpreted scripting language. C is a very popular compilation language. Because of its compilation nature, C is generally faster than Python, but it is more underlying. Python programming is faster and easier. Note: In the currently most authoritative tibench programming language ranking, the Python running efficiency and C language ranking are 6th and 2nd respectively, in the scripting and compilation languages, the second place is the first place, and the second place is PHP and Java ).

The problem is, if the Python program does not have input,) Is it more cost-effective than C in terms of the additional time spent in the runtime, and whether the time spent in the runtime is more important than the programming time.
System Program

I decided to create a simple program that can handle the calculation of the following formula:

 
 
  1. { x + y = 14 
  2. { x^2 + y^2 = 100 

I quickly wrote it in Python and found the answer. Then, I translate it into C language. I know that the same program in C language will spend more code than Python, but this is not a problem I pay attention. Before proceeding, let's look at the Code:

 
 
  1. Python:  
  2. x = 1 
  3. while x <= 14:  
  4. y = 14 - x  
  5. print str(x) + "|" + str(y)  
  6. if x**2 + y**2 == 100:  
  7. print "match"  
  8. xx = x + 1  
  9. C:  
  10. #include (<)stdio.h(>)  
  11. int main()  
  12. {  
  13. int x, y, t;  
  14. for (x = 1; x <= 14; x++) {  
  15. y = 14 - x;  
  16. printf("%d|%d\n", x, y);  
  17. if ((x*x) + (y*y) == 100)  
  18. printf("match\n");  
  19. }  
  20. return 0;  
  21. }  
  22.  

I have always heard that C is always one of the fastest languages. I have not seen any difference in the speed of running these two programs on the command port. So I open the Ubuntu command port and enter the following code:

 
 
  1. time ./a.out 

The time Command is followed by a common command that can be run independently from the time command. It runs the command and timing-obviously, the test object here is a C program .) I get 0.001 seconds of real time), while the user time is used) and the system time is used) are both 0. Next, the Python running efficiency:

 
 
  1. time python system.py 

The result is a little scary: 0.017 seconds real time, 0.012 seconds user time, 0.004 seconds system time. Of course, the difference between real time is only 16 milliseconds after all, but this difference is obvious in large systems that run a large number of operations for a long time.
Millions of Programs

I decided to put this idea into reality. I wrote another program, which prints all integers from 0 to 1000000, including 0. Of course, this is different from the large number of operations I mentioned earlier, but it still gives computers more print content.

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.