Skip to content

Instantly share code, notes, and snippets.

/cyt.py

Created Nov 7, 2012
Embed
What would you like to do?
cython example
if __name__ == '__main__':
import pyximport; pyximport.install(pyimport=True)
from v2d import v2d
# I am lazy to 'compyle' by cython and it takes some time to do it on
# import so i'm measuring time after import
from time import time
time0 = time()
def test():
for i in range(200):
v1 = v2d(2, 3**i)
v1.angle += 13
v2 = v2d(4**i, 42)
v2.length -= 14
for i in range(5000):
test()
print time() - time0
if __name__ == '__main__':
from v2d import v2d
from time import time
time0 = time()
def test():
for i in range(200):
v1 = v2d(2, 3**i)
v1.angle += 13
v2 = v2d(4**i, 42)
v2.length -= 14
for i in range(5000):
test()
print time() - time0
import operator
import math
class v2d(object):
def __init__(self, x, y=None):
self.__x = 0
self.__y = 0
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()
@property
def x(self):
return self.__x
@property
def y(self):
return self.__y
def get_length(self):
return math.sqrt(self.__x**2 + self.__y**2)
def set_length(self, len):
l = self.get_length()
if l:
self.__x /= l
self.__y /= l
self.__x = self.__x * len
self.__y = self.__y * len
length = property(get_length, set_length)
def get_angle(self):
if ((self.__x**2 + self.__y**2) == 0):
return 0
return math.degrees(math.atan2(self.__y, self.__x))
def set_angle(self, angle):
self.__x = self.get_length()
self.__y = 0
r = math.radians(angle)
c = math.cos(r)
s = math.sin(r)
nx = self.__x*c - self.__y*s
ny = self.__x*s + self.__y*c
self.__x = nx
self.__y = ny
angle = property(get_angle, set_angle)
import cython
import operator
import math
class v2d(object):
def __init__(self, x, y=None):
self.__x = 0
self.__y = 0
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()
@property
def x(self):
return self.__x
@property
def y(self):
return self.__y
@cython.returns(cython.float)
def get_length(self):
return math.sqrt(self.__x**2 + self.__y**2)
@cython.locals(l=cython.float)
def set_length(self, len):
l = self.get_length()
if l:
self.__x /= l
self.__y /= l
self.__x = self.__x * len
self.__y = self.__y * len
length = property(get_length, set_length)
@cython.returns(cython.float)
def get_angle(self):
if ((self.__x**2 + self.__y**2) == 0):
return 0
return math.degrees(math.atan2(self.__y, self.__x))
@cython.locals(r=cython.float, c=cython.float, s=cython.float)
def set_angle(self, angle):
self.__x = self.get_length()
self.__y = 0
r = math.radians(angle)
c = math.cos(r)
s = math.sin(r)
self.__x = self.__x*c - self.__y*s
self.__y = self.__x*s + self.__y*c
angle = property(get_angle, set_angle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment