Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/speed_recursion.py
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/806773f5325cabb96d4d to your computer and use it in GitHub Desktop.
Save zeffii/806773f5325cabb96d4d to your computer and use it in GitHub Desktop.
from timeit import Timer
def test(setup, r=3, times=100):
t1 = Timer("""w = recurse_fxy(ul,v1,cross,2)""", setup=setup)
print("fxy 1000:1000:", min(t1.repeat(r,times)))
t2 = Timer("""w = recurse_fxy2(ul,v1,cross,2)""", setup=setup)
print("fxy2 1000:1000:", min(t2.repeat(r,times)))
t3 = Timer("""w = recurse_fxy(ul,v2,cross,2)""", setup=setup)
print("fxy : 1000:1", min(t3.repeat(r,times)))
t4 = Timer("""w = recurse_fxy2(ul,v2,cross,2)""", setup=setup)
print("fxy2: 1000:1", min(t4.repeat(r,times)))
path = 'c:/blender_trunk/2.71/scripts/addons/sverchok-master/tests/speed_recursion.py'
with open(path) as setup:
setup = ''.join(setup.readlines())
print('---')
test(setup)
from mathutils import Vector
from random import random
ul = [[[random(),random(),random()] for i in range(1000)]]
v1 = [[[random(),random(),random()] for i in range(1000)]]
v2 = [[[0,0,1]]]
def fullList(l, count):
d = count - len(l)
if d > 0:
l.extend([l[-1] for a in range(d)])
return
cross = lambda u,v:Vector(u).cross(v)[:]
def recurse_fxy( l1, l2, f, leve):
if not leve:
return f(l1, l2)
else:
max_obj = max(len(l1), len(l2))
fullList(l1, max_obj)
fullList(l2, max_obj)
return [recurse_fxy(l1[i], l2[i], f, leve-1) for i in range(len(l1))]
def recurse_fxy2(l1, l2, f, leve):
if leve==1:
max_obj = max(len(l1),len(l2))
res = []
res_append = res.append
for i,obj in enumerate(zip(l1,l2)):
res_append(f(obj[0], obj[1]))
if i < max_obj-1:
if len(l1)-1==i:
v_l1=l1[-1]
for i in range(i,max_obj):
res_append(f(v_l1,l2[i]))
else:
v_l2 = l2[-1]
for i in range(i,max_obj):
res_append(f(l1[i], v_l2))
return res
elif leve > 1:
max_obj = max(len(l1),len(l2))
fullList(l1, max_obj)
fullList(l2, max_obj)
res = []
for i in range(len(l1)):
res.append(recurse_fxy2(l1[i], l2[i],f, leve-1))
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment