Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created June 19, 2014 22:00
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/8828f1b68bb4d782067b to your computer and use it in GitHub Desktop.
Save zeffii/8828f1b68bb4d782067b to your computer and use it in GitHub Desktop.
from timeit import Timer
# setup = """\
from mathutils import Vector
from random import random
from itertools import zip_longest
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_fxy2(l1, l2, f, leve):
res = []
res_append = res.append
fl = l2[-1] if len(l1) > len(l2) else l1[-1]
if leve == 1:
for u, v in zip_longest(l1, l2, fillvalue=fl):
res_append(f(u, v))
else:
for u, v in zip_longest(l1, l2, fillvalue=fl):
res.append(recurse_fxy2(u, v, f, leve - 1))
return res
def recurse_fxy3(l1, l2, leve):
res = []
res_append = res.append
fl = l2[-1] if len(l1) > len(l2) else l1[-1]
if leve == 1:
for u, v in zip_longest(l1, l2, fillvalue=fl):
res_append({func})
else:
for u, v in zip_longest(l1, l2, fillvalue=fl):
res.append(recurse_fxy3(u, v, leve - 1))
return res
# """
functors = {'cross': "Vector(u).cross(v)[:]"}
setup = setup.format(func=functors['cross'])
def test(r=3, times=100):
t2 = Timer("""w = recurse_fxy2(ul,v1,cross,2)""", setup=setup)
print("zipL 1000:1000:", min(t2.repeat(r, times)))
t6 = Timer("""w = recurse_fxy3(ul,v1, 2)""", setup=setup)
print("ugly: 1000:1000", min(t6.repeat(r, times)))
t4 = Timer("""w = recurse_fxy2(ul,v2,cross,2)""", setup=setup)
print("zipL: 1000:1", min(t4.repeat(r, times)))
t5 = Timer("""w = recurse_fxy3(ul,v2, 2)""", setup=setup)
print("ugly: 1000:1", min(t5.repeat(r, times)))
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment