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?
-
- # Include <stdio. h>
-
- # Include <time. h>
-
-
- IntMain (IntArgc,Char* Argv [])
-
- {
-
- UnsignedLongI = 1;
- UnsignedLongUlnum = 1000000;
-
-
- Clock_tStart, finish;
-
- DoubleDuration;
-
-
- Start = clock ();
-
-
- While(Ulnum! = 0)
-
- {
-
- Printf ("\ Nthe ulnum is: % u", I );
- Ulnum --;
-
- I ++;
-
- }
-
-
- Finish = clock ();
-
- Duration = (Double) (Finish-Start)/clocks_per_sec;
-
- Printf ("\ N use time: % F seconds \ n", Duration );
-
-
- System ("Pause");
-
- Return0;
-
- }
# 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?
-
- #! /Usr/bin/ENV Python
-
- #-*-Coding: UTF-8 -*-
-
-
- ImportTime
-
- ImportOS
-
-
- Time_begin = time. Clock ()
-
-
- I =1
-
- Ulnum =1000000
- While(Ulnum! =0):
-
- Print "The ulnum is: % u"% I
-
- Ulnum-=1
-
- I + =1
-
- Print "Use Time: % s"% (Time. Clock ()-time_begin)
-
- 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?
-
- // Test_dll.cpp: defines the entry point for the DLL application.
-
- //
-
-
- # Include "stdafx. H"
-
- # Include <stdio. h>
-
-
- BoolApientry dllmain (HandleHmodule,
-
- DWORDUl_reason_for_call,
-
- LpvoidLpreserved
-
- )
-
- {
-
- ReturnTrue;
-
- }
-
-
- Extern "C"_ Declspec (Dllexport)VoidPrint_sum (unsignedLongUlnum)
-
- {
-
- UnsignedLongI = 1;
-
-
- While(Ulnum! = 0)
-
- {
-
- Printf ("\ Nthe ulnum is: % u", I );
- Ulnum --;
-
- I ++;
-
- }
-
-
- }
// 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?
- Import OS
- Import ctypes
- Import time
-
-
- time_begin = time. Clock ()
-
- test_dll = ctypes. cdll. loadlibrary ( 'test _ DLL. dll ')
-
- test_dll.print_sum ( 1000000 )
-
- Print "Use Time: % s" % (time. clock ()-time_begin)
-
- 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.