A closure occurs when a function has access to a local variable from an enclosing scope that has finished its execution.
def make_printer(msg): def printer(): print msg return printer
printer = make_printer('Foo!') printer()
import numpy as np | |
from numba import double | |
from numba.decorators import jit | |
@jit(argtypes=[double[:,:], double[:,:]]) | |
def pairwise_numba(X, D): | |
M = X.shape[0] | |
N = X.shape[1] | |
for i in range(M): | |
for j in range(M): |
from numba import autojit | |
import numpy as np | |
@autojit | |
def sum1d(my_double_array): | |
sum = 0.0 | |
for i in range(my_double_array.shape[0]): | |
sum += my_double_array[i] | |
return sum |
from numba import jit | |
import numpy as np | |
@jit('f8(f8[:])') | |
def sum1d(my_double_array): | |
sum = 0.0 | |
for i in range(my_double_array.shape[0]): | |
sum += my_double_array[i] | |
return sum |
A closure occurs when a function has access to a local variable from an enclosing scope that has finished its execution.
def make_printer(msg): def printer(): print msg return printer
printer = make_printer('Foo!') printer()
from numpy import zeros | |
from scipy import weave | |
import numpy as np | |
import time | |
import timeit | |
import scipy.weave as weave | |
import pyximport | |
pyximport.install(setup_args={'include_dirs':[np.get_include()]}) | |
dx = 0.1 |
In [328]: timeit.Timer('if done is None:pass','done = 1').timeit() | |
Out[328]: 0.09523200988769531 | |
In [329]: timeit.Timer('if done!=None:pass','done = 1').timeit() | |
Out[329]: 0.2190079689025879 |
my_sum: 0.0396 | |
np.sum: 0.0396 | |
sum: 5.47 |
# -*- coding: utf-8 -*- | |
""" | |
使用weave将C++嵌入到Python程序中,加快程序的运行。 | |
""" | |
import scipy.weave as weave | |
import numpy as np | |
import time | |
def my_sum(a): | |
n=int(len(a)) |
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import time | |
import math | |
import numpy as np | |
x = [i * 0.001 for i in xrange(1000000)] | |
start = time.clock() | |
for i,t in enumerate(x): | |
x[i] = math.sin(t) |
the loop of the math.sin: 0.78 | |
math.sin: 0.56 | |
numpy.sin: 0.08 | |
the for loop of numpy.sin 3.66 |