0%

Speed up your python

One can write efficient python code, but it is very hard to beat build-in functions which are written by C.
To speed up the python program, rewrite them in C as python module will increase the performance w/o any doubt.
But that’s not the only way as we have JIT tech nowadays. pypy and numba(llvm) are the shinning stars in this area.

Pure python code for monte carlo PI simulation

1
2
3
4
5
6
7
8
9
def monte_carlo_pi(samples):
acc = 0
for i in range(samples):
x = random.random()
y = random.random()
if (x ** 2 + y ** 2) <= 1.0:
acc += 1

return 4.0 * acc / samples

Load 3rd party dynamic library in python (No need for 3rd party source code)

Like JNA solution in java, python has its own implementation: ctypes or cffi
The 3rd party library may NOT aware it will be ran in python environment.

3rd party shared library check

In windows world, we can use VC toolset dumpbin to check exported functions:
if you forget to add __declspec(dllexport) before the function definition,
the function cannot be used on other program…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
C:\> dumpbin pi.dll /EXPORTS
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file pi.dll

File Type: DLL

Section contains the following exports for pi.dll

00000000 characteristics
6048347E time date stamp Wed Mar 10 10:52:46 2021
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 00006AB0 monte_carlo_pi

Summary

3000 .data
1000 .pdata
3000 .rdata
1000 .reloc
A000 .text

And in GCC world, we can use nm to check exported functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ nm libpi.so -D
0000000000201030 B __bss_start
w __cxa_finalize
0000000000201030 D _edata
0000000000201038 B _end
000000000000083c T _fini
U gettimeofday
w __gmon_start__
00000000000005a8 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses
0000000000000705 T monte_carlo_pi
U rand
U srand

together with the .h file, it will form the regular C world for programmers

With above background knowledge, we can check if the shared library be good to use in python