mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-26 19:42:24 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -17,7 +17,7 @@ communication between `py + c` | `import fact`
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file fact.h
|
||||
#ifndef FACT_H
|
||||
#define FACT_h
|
||||
@@ -26,14 +26,14 @@ int fact(int n);
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing fact.h
|
||||
|
||||
```
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file fact.c
|
||||
#include "fact.h"
|
||||
int fact(int n)
|
||||
@@ -44,7 +44,7 @@ int fact(int n)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing fact.c
|
||||
|
||||
```
|
||||
@@ -53,7 +53,7 @@ Writing fact.c
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file fact_wrap.c
|
||||
|
||||
/* Must include Python.h before any standard headers*/
|
||||
@@ -89,7 +89,7 @@ initexample(void)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing fact_wrap.c
|
||||
|
||||
```
|
||||
@@ -98,7 +98,7 @@ Writing fact_wrap.c
|
||||
|
||||
手动使用 `gcc` 编译,`Windows` 下如果没有 `gcc`,可以通过 `conda` 进行安装:
|
||||
|
||||
```
|
||||
```py
|
||||
conda install mingw4
|
||||
```
|
||||
|
||||
@@ -106,14 +106,14 @@ conda install mingw4
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
!gcc -DMS_WIN64 -c fact.c fact_wrap.c -IC:\Miniconda\include
|
||||
|
||||
```
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
!gcc -DMS_WIN64 -shared fact.o fact_wrap.o -LC:\Miniconda\libs -lpython27 -o example.pyd
|
||||
|
||||
```
|
||||
@@ -124,14 +124,14 @@ In [5]:
|
||||
|
||||
* `Windows 32-bit`
|
||||
|
||||
```
|
||||
```py
|
||||
gcc -c fact.c fact_wrap.c -I<your python path>\include
|
||||
gcc -shared fact.o fact_wrap.o -L<your python path>\libs -lpython27 -o example.pyd
|
||||
```
|
||||
|
||||
* `Unix`
|
||||
|
||||
```
|
||||
```py
|
||||
gcc -c fact.c fact_wrap.c -I<your python path>
|
||||
gcc -shared fact.o fact_wrap.o -L<your python path>\config -lpython27 -o example.so
|
||||
```
|
||||
@@ -142,13 +142,13 @@ In [5]:
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
import example
|
||||
print dir(example)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
['__doc__', '__file__', '__name__', '__package__', 'fact']
|
||||
|
||||
```
|
||||
@@ -157,12 +157,12 @@ print dir(example)
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
print 'factorial of 10:', example.fact(10)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
factorial of 10: 3628800
|
||||
|
||||
```
|
||||
@@ -173,7 +173,7 @@ factorial of 10: 3628800
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
!rm -f example.pyd
|
||||
|
||||
```
|
||||
@@ -182,7 +182,7 @@ In [8]:
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file setup.py
|
||||
from distutils.core import setup, Extension
|
||||
|
||||
@@ -192,14 +192,14 @@ setup(name='example', ext_modules=[ext])
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing setup.py
|
||||
|
||||
```
|
||||
|
||||
使用 `distutils` 中的函数,我们进行 `build` 和 `install`:
|
||||
|
||||
```
|
||||
```py
|
||||
python setup.py build (--compiler=mingw64)
|
||||
python setup.py install
|
||||
```
|
||||
@@ -210,12 +210,12 @@ python setup.py install
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
!python setup.py build_ext --inplace
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
running build_ext
|
||||
building 'example' extension
|
||||
creating build
|
||||
@@ -234,14 +234,14 @@ C:\Miniconda\Scripts\gcc.bat -DMS_WIN64 -shared -s build\temp.win-amd64-2.7\Rele
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
import example
|
||||
|
||||
print 'factorial of 10:', example.fact(10)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
factorial of 10: 3628800
|
||||
|
||||
```
|
||||
@@ -250,7 +250,7 @@ factorial of 10: 3628800
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
def pyfact(n):
|
||||
if n <= 1: return 1
|
||||
return n * pyfact(n-1)
|
||||
@@ -260,7 +260,7 @@ print example.fact(10)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
3628800
|
||||
3628800
|
||||
|
||||
@@ -270,12 +270,12 @@ print example.fact(10)
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
%timeit example.fact(10)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
The slowest run took 13.17 times longer than the fastest. This could mean that an intermediate result is being cached
|
||||
1000000 loops, best of 3: 213 ns per loop
|
||||
|
||||
@@ -283,12 +283,12 @@ The slowest run took 13.17 times longer than the fastest. This could mean that a
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
%timeit pyfact(10)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1000000 loops, best of 3: 1.43 µs per loop
|
||||
|
||||
```
|
||||
@@ -297,14 +297,14 @@ In [14]:
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
example.fact(100)
|
||||
|
||||
```
|
||||
|
||||
Out[15]:
|
||||
|
||||
```
|
||||
```py
|
||||
0
|
||||
```
|
||||
|
||||
@@ -312,14 +312,14 @@ Out[15]:
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
pyfact(100)
|
||||
|
||||
```
|
||||
|
||||
Out[16]:
|
||||
|
||||
```
|
||||
```py
|
||||
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L
|
||||
```
|
||||
|
||||
@@ -327,7 +327,7 @@ Out[16]:
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
import zipfile
|
||||
|
||||
f = zipfile.ZipFile('07-02-example.zip','w',zipfile.ZIP_DEFLATED)
|
||||
@@ -344,7 +344,7 @@ f.close()
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
!rm -f fact*.*
|
||||
!rm -f example.*
|
||||
!rm -f setup*.*
|
||||
|
||||
Reference in New Issue
Block a user