This is why I chose C language instead of python

1. The reason why c is faster than python

        C is a compiled language. The compiler directly compiles the source code of C into machine language for running. Compared with interpreted languages ​​​​such as Python and Java, it reduces the time of runtime interpretation and translation and improves operating efficiency. Secondly, C language is not as good as Java. The garbage collection mechanism of the language needs to be released by itself to reduce its own internal consumption and improve its execution efficiency.

7ea49dbeca7f444e8108de7a485d1823.png

 2. Experimental proof

        ​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ With the C language and python language are used are to perform cumulative sums of specified sizes to compare the running time efficiency

  • C code implementation
#include<stdio.h>
#include<time.h>
int add(int num) 
{	
    int sum = 0;
    for (int i = 0; i < num; i++)
    {
        sum +=i;
    }
    
    return sum;
}
int main() {
    clock_t start, end;
    start = clock();
    add(1000000000);
    end = clock();
    printf("%f seconds\n",(double)(end - start) / CLOCKS_PER_SEC);
}
  •  Python code implementation
import time
def test1(num):
    sum = 0
    for x in range(num):
        sum+=x
    return sum
if __name__ == '__main__':
    print("测试python代码")
    start = time.time()
    test1(1000000000)
    end = time.time()
    print (end-start)

3. Result

        The following test results are in seconds. In fact, the results are still amazing. Everyone may know that the execution speed of C language is much higher than that of Python, but they did not expect it to be so much faster, almost 25 times the difference!

language operation hours
C 2.295000 seconds
Python 52.89267 seconds

4. How Python uses C code to improve computing efficiency

        If the requirements need to be implemented only by Python, but you need to pursue the execution speed of the C language, what should I do? In fact, there are still ways to solve it. At present, I know the following methods to increase the speed of Python:

  • Optimize code
  • Multi-threaded parallel computing
  • Precompile py files to pyc
  • Dynamic library or static library calls C

5. Extension (how python calls c-compiled dynamic library files)

        Microsoft or gcc, mingw and other compilers have been installed under Windows. I will not explain the installation steps too much here. Baidu has many installation tutorials. Make sure that after installation, we need to create an add.c file, and then attach the following code:

#include<stdio.h>
int add(int num) 
{	
    int sum = 0;
    for (int i = 0; i < num; i++)
    {
        sum +=i;
    }
    return sum;
}

        After writing the code and ensuring that it is runnable, we need to make it into a dynamic library file dll that can be run by windos (.so file under Linux)

# Compile dynamic library
gcc -c add.c -o add.o -fPIC
gcc -shared -o libadd.dll add. o

        After successful compilation, a .dll dynamic library file will be generated in this folder. We need to use python to call the dynamic library file and use the add sum function.

# python调用
from ctypes import *
def test2():
    add = CDLL('libadd.dll')
    nResult = add.add(10000000)

         Finally, we compare python’s native summation with the summation that calls the c dynamic library

import time
from ctypes import *
add1 = CDLL('libadd.dll')
def test1(num):
    sum = 0
    for x in range(num):
        sum+=x
    return sum
def test2(num):
    nResult = add1.add(num)
if __name__ == '__main__':
    print("测试python代码")
    start = time.time()
    test1(1000000000)
    end = time.time()
    print (end-start)
    print("测试动态库代码")
    start1 = time.time()
    test2(1000000000)
    end2 = time.time()
    print (end2-start1)

        The results of the comparison are as follows:

38c51c9c54304637826389323f270c4b.png

 6. Briefly talk about the advantages of python

        Although the execution efficiency of Python is much lower than that of C language, I personally still like the language Python very much, and currently Python has good application scenarios in different directions: crawlers, artificial intelligence, automated testing, etc. are all good ~

 

Finally, if you like it, just click a heart! Thank you for your support~

 

 

Guess you like

Origin blog.csdn.net/m0_43432638/article/details/126030084