Python: compare the performance of C and python by executing 1 million prints, and use C and python to solve the performance problem.

Source: Internet
Author: User

Python is a dynamic language with high development efficiency. But as we know, the execution efficiency of dynamic languages is often relatively low. Please refer to the following simple test process:

I. 1 million printing times in C language:

Code:

[CPP] View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include <time. h>
  3. IntMain (IntArgc,Char* Argv [])
  4. {
  5. UnsignedLongI = 1;
  6. UnsignedLongUlnum = 1000000;
  7. Clock_tStart, finish;
  8. DoubleDuration;
  9. Start = clock ();
  10. While(Ulnum! = 0)
  11. {
  12. Printf ("\ Nthe ulnum is: % u", I );
  13. Ulnum --;
  14. I ++;
  15. }
  16. Finish = clock ();
  17. Duration = (Double) (Finish-Start)/clocks_per_sec;
  18. Printf ("\ N use time: % F seconds \ n", Duration );
  19. System ("Pause");
  20. Return0;
  21. }

# Include <stdio. h> <br/> # include <time. h> </P> <p> int main (INT argc, char * argv []) <br/> {<br/> unsigned long I = 1; <br/> unsigned long ulnum = 1000000; </P> <p> clock_t start, finish; <br/> double duration; </P> <p> Start = clock (); </P> <p> while (ulnum! = 0) <br/>{< br/> printf ("\ nthe ulnum is: % u", I); <br/> ulnum --; <br/> I ++; <br/>}</P> <p> finish = clock (); <br/> duration = (double) (finish-start) /clocks_per_sec; <br/> printf ("\ n use time: % F seconds \ n", duration); </P> <p> system ("pause "); </P> <p> return 0; <br/>}< br/>

Test:

It can be seen that the execution took about 489 seconds.

Ii. Python implements 1 million printing times:

Code:

[Python] View plaincopyprint?
  1. #! /Usr/bin/ENV Python
  2. #-*-Coding: UTF-8 -*-
  3. ImportTime
  4. ImportOS
  5. Time_begin = time. Clock ()
  6. I =1
  7. Ulnum =1000000
  8. While(Ulnum! =0):
  9. Print "The ulnum is: % u"% I
  10. Ulnum-=1
  11. I + =1
  12. Print "Use Time: % s"% (Time. Clock ()-time_begin)
  13. OS. System ("Pause")

#! /Usr/bin/ENV python <br/> #-*-coding: UTF-8-*-</P> <p> Import time <br/> Import OS </P> <p> time_begin = time. clock () </P> <p> I = 1 <br/> ulnum = 1000000 <br/> while (ulnum! = 0): <br/> Print "The ulnum is: % u "% I <br/> ulnum-= 1 <br/> I + = 1 <br/> Print" Use Time: % s "% (time. clock ()-time_begin) <br/> OS. system ("pause") <br/>

Test:

It can be seen that the execution took about 675 seconds.

 

Iii. Solution to performance problems:

Through the comparison above, we can see that the sameAlgorithmThe difference between C and Python execution is more than 180 seconds, so we need a solution to make programming both Python-like development efficiency and C-like execution efficiency, so we thought thatProgramThe statement that consumes the most performance in C will solve this problem better. Of course, there may be many implementation methods. This article only uses python to call the DLL method, other methods will be analyzed later.

The idea is as follows:

1. Implement the loop part of the program in C and encapsulate it as a DLL;

2. Call this DLL in Python for computation;

Solution implementation:

1. Create a DLL. You can easily create a DLL using VC. The Code is as follows. compile it to generate a file named test_dll.dll. compile it into the release version.

[CPP] View plaincopyprint?
  1. // Test_dll.cpp: defines the entry point for the DLL application.
  2. //
  3. # Include "stdafx. H"
  4. # Include <stdio. h>
  5. BoolApientry dllmain (HandleHmodule,
  6. DWORDUl_reason_for_call,
  7. LpvoidLpreserved
  8. )
  9. {
  10. ReturnTrue;
  11. }
  12. Extern "C"_ Declspec (Dllexport)VoidPrint_sum (unsignedLongUlnum)
  13. {
  14. UnsignedLongI = 1;
  15. While(Ulnum! = 0)
  16. {
  17. Printf ("\ Nthe ulnum is: % u", I );
  18. Ulnum --;
  19. I ++;
  20. }
  21. }

// Test_dll.cpp: defines the entry point for the DLL application. <br/> // </P> <p> # include "stdafx. H "<br/> # include <stdio. h> </P> <p> bool apientry dllmain (handle hmodule, <br/> DWORD ul_reason_for_call, <br/> lpvoid lpreserved <br/>) <br/>{< br/> return true; <br/>}</P> <p> extern "C" _ declspec (dllexport) void print_sum (unsigned long ulnum) <br/>{< br/> unsigned long I = 1; </P> <p> while (ulnum! = 0) <br/>{< br/> printf ("\ nthe ulnum is: % u", I); <br/> ulnum --; <br/> I ++; <br/>}</P> <p >}< br/>

2. Use python to call the test_dll.dll file. The Code is as follows:

[Python] View plaincopyprint?
    1. Import OS
    2. Import ctypes
    3. Import time
    4. time_begin = time. Clock ()
    5. test_dll = ctypes. cdll. loadlibrary ( 'test _ DLL. dll ')
    6. test_dll.print_sum ( 1000000 )
    7. Print "Use Time: % s" % (time. clock ()-time_begin)
    8. OS. System ( "pause" )

Import OS <br/> Import ctypes <br/> Import time </P> <p> time_begin = time. clock () </P> <p> test_dll = ctypes. cdll. loadlibrary ('test _ DLL. dll ') </P> <p> test_dll.print_sum (1000000) </P> <p> Print "Use Time: % s" % (time. clock ()-time_begin) </P> <p> OS. system ("pause") <br/>

3. Test the execution time:

This time we spent 507 seconds, and we can see that the result is similar to that of the C program.

Iv. Postscript

Calling DLL files through pyhton is only one way to improve performance, such as C and Python mixed programming. For details, see the following blog.

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.