Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
cimport cython
import operator
import math
cdef extern from "math.h":
double sqrt(double x)
double atan2(double y, double x)
double cos(double x)
double sin(double x)
double floor(double x)
cdef class v2d(object):
cdef public double x, y
def __init__(self, x, y=None):
if x is not None and y is not None:
self.x = float(x)
self.y = float(y)
elif type(x) == v2d:
self.x = x.x
self.y = x.y
elif type(x) == list or type(x) == tuple:
self.x = x[0]
self.y = x[1]
else:
raise TypeError()
# Position
def get_pos(self):
return (self.x, self.y)
def set_pos(self,pos):
self.x = float(pos.x)
self.y = float(pos.y)
pos = property(get_pos, set_pos)
def get_int_pos(self):
cdef int x, y
x = int(floor(self.x))
y = int(floor(self.y))
return(x,y)
ipos = property(get_int_pos, set_pos)
# ...
imp@home ~/zz/cython $ python pyt.py
12.2809770107
imp@home ~/zzz/cython $ python cyt.py
10.8371829987
cimport cython
cdef class v2d(object):
cdef public float __x, __y
imp@home ~/zzz/cython $ python cyt.py
8.40173888206
...
8.0
cimport cython
cdef class v2d(object):
cdef public float __x, __y
cpdef float get_length(self)
@cython.cfunc
@cython.returns(cython.float)
@cython.locals(x=cython.float, y=cython.float)
def __cget_length(self, x, y):
pass
@cython.cfunc
@cython.returns(cython.float)
@cython.locals(x=cython.float, y=cython.float, z=cython.float)
def __ccomplex_calcs(self, x, y):
z = self.__cget_length(x, y) ** 8 * self.__cget_anima()
def get_length(self, x, y):
return self.__cget_length(x, y)
def complex_calcs(self, x,y):
return self.__ccomplex_calcs( x, y)
$ for power in 2 4 6 ; do for module in timing_{native,ctypes,cython,cython_range} ; do echo -n "$module: $power "; python2 -m timeit --setup "from $module import test" "test(10 ** $power)" ; done ; echo "---------" ; done
timing_native: 2 100000 loops, best of 3: 4.72 usec per loop
timing_ctypes: 2 100000 loops, best of 3: 2.27 usec per loop
timing_cython: 2 1000000 loops, best of 3: 0.21 usec per loop
timing_cython_range: 2 1000000 loops, best of 3: 0.211 usec per loop
---------
timing_native: 4 1000 loops, best of 3: 448 usec per loop
timing_ctypes: 4 10000 loops, best of 3: 30.1 usec per loop
timing_cython: 4 100000 loops, best of 3: 5.57 usec per loop
timing_cython_range: 4 100000 loops, best of 3: 6 usec per loop
---------
timing_native: 6 10 loops, best of 3: 248 msec per loop
timing_ctypes: 6 100 loops, best of 3: 2.59 msec per loop
timing_cython: 6 1000 loops, best of 3: 541 usec per loop
timing_cython_range: 6 1000 loops, best of 3: 586 usec per loop
---------
#include <boost/python.hpp>
using namespace boost::python;
namespace Test
{
unsigned long long sumrange(unsigned long long arg)
{
unsigned long long i, x;
x = 0;
for (i = 0; i < arg; i++) {
x = x + i;
}
return x;
}
}
BOOST_PYTHON_MODULE(boost_python_module)
{
def("sumrange", Test::sumrange);
}
import timeit
from boost_python_module import sumrange as sumrange2
def sumrange1(arg):
return sum(xrange(arg))
for power in (2, 8, 9):
loops = 10**power
t = timeit.Timer(lambda : sumrange1(loops))
print("Loops: {0} Time: {1}".format(loops, t.timeit(1)))
print()
for power in (2, 8, 9):
loops = 10**power
t = timeit.Timer(lambda : sumrange2(loops))
print("Loops: {0} Time: {1}".format(loops, t.timeit(1)))
Производительность в Python. Легкий путь