mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-24 02:23:45 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
考虑之前的斐波拉契数列,`Python` 版本:
|
||||
|
||||
```
|
||||
```py
|
||||
def fib(n):
|
||||
a,b = 1,1
|
||||
for i in range(n):
|
||||
@@ -17,7 +17,7 @@ def fib(n):
|
||||
|
||||
`C` 版本:
|
||||
|
||||
```
|
||||
```py
|
||||
int fib(int n) {
|
||||
int tmp, i, a, b;
|
||||
a = b = 1;
|
||||
@@ -31,7 +31,7 @@ int fib(int n) {
|
||||
|
||||
`Cython` 版本:
|
||||
|
||||
```
|
||||
```py
|
||||
def fib(int n):
|
||||
cdef int i, a, b
|
||||
a,b = 1,1
|
||||
@@ -60,7 +60,7 @@ def fib(int n):
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
%load_ext Cython
|
||||
|
||||
```
|
||||
@@ -69,7 +69,7 @@ In [1]:
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%cython
|
||||
def cyfib(int n):
|
||||
cdef int i, a, b
|
||||
@@ -82,14 +82,14 @@ def cyfib(int n):
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
cyfib(10)
|
||||
|
||||
```
|
||||
|
||||
Out[3]:
|
||||
|
||||
```
|
||||
```py
|
||||
144
|
||||
```
|
||||
|
||||
@@ -99,7 +99,7 @@ Out[3]:
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file fib.pyx
|
||||
def cyfib(int n):
|
||||
cdef int i, a, b
|
||||
@@ -110,14 +110,14 @@ def cyfib(int n):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing fib.pyx
|
||||
|
||||
```
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file setup.py
|
||||
from distutils.core import setup
|
||||
from distutils.extension import Extension
|
||||
@@ -129,7 +129,7 @@ setup(ext_modules=[ext], cmdclass={'build_ext': build_ext})
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Overwriting setup.py
|
||||
|
||||
```
|
||||
@@ -138,12 +138,12 @@ Overwriting setup.py
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
!python setup.py build_ext --inplace
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
running build_ext
|
||||
cythoning fib.pyx to fib.c
|
||||
building 'fib' extension
|
||||
@@ -160,7 +160,7 @@ C:\Miniconda\Scripts\gcc.bat -DMS_WIN64 -shared -s build\temp.win-amd64-2.7\Rele
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
import fib
|
||||
|
||||
fib.cyfib(10)
|
||||
@@ -169,13 +169,13 @@ fib.cyfib(10)
|
||||
|
||||
Out[7]:
|
||||
|
||||
```
|
||||
```py
|
||||
144
|
||||
```
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
import zipfile
|
||||
|
||||
f = zipfile.ZipFile('07-03-fib.zip','w',zipfile.ZIP_DEFLATED)
|
||||
@@ -194,7 +194,7 @@ f.close()
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
!rm -f fib.pyd
|
||||
!rm -f fib.pyc
|
||||
!rm -f fib.C
|
||||
@@ -205,7 +205,7 @@ In [9]:
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
%reset -f
|
||||
|
||||
```
|
||||
@@ -214,7 +214,7 @@ In [10]:
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
import pyximport
|
||||
pyximport.install()
|
||||
|
||||
@@ -226,7 +226,7 @@ fib.cyfib(10)
|
||||
|
||||
Out[11]:
|
||||
|
||||
```
|
||||
```py
|
||||
144
|
||||
```
|
||||
|
||||
@@ -236,7 +236,7 @@ Out[11]:
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
!rm -f setup*.*
|
||||
!rm -f fib.*
|
||||
!rm -rf build
|
||||
|
||||
Reference in New Issue
Block a user