Skip to content

Instantly share code, notes, and snippets.

@ly29
Last active August 29, 2015 14:02
Show Gist options
  • Save ly29/c2b714b91b068ac9d914 to your computer and use it in GitHub Desktop.
Save ly29/c2b714b91b068ac9d914 to your computer and use it in GitHub Desktop.
from timeit import Timer
setup_user = """
from math import sqrt
a= [(i,i,i) for i in range(1000)]
def len_user(v):
return sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2])
"""
setup_vector = """
from mathutils import Vector
a= [Vector((i,i,i)) for i in range(1000)]
def len_vec(v):
return v.length
"""
setup_lambda = """
from mathutils import Vector
a= [Vector((i,i,i)) for i in range(1000)]
len_lambda=lambda a:a.length
"""
setup_np = """
import numpy as np
from numpy.linalg import norm
a = np.array([range(1000),range(1000),range(1000)])
def len_np(v):
return norm(v,axis=0)
"""
def test():
t1 = Timer("""m = [len_user(v) for v in a]""", setup=setup_user)
print("user ", min(t1.repeat(3,1000)))
t2 = Timer("""m =[len_vec(v) for v in a] """, setup=setup_vector)
print("Vector", min(t2.repeat(3,1000)))
t3 = Timer("""m=[len_lambda(v) for v in a]""", setup=setup_lambda)
print("Lambda", min(t3.repeat(3,1000)))
t4 = Timer("""m = len_np(a)""", setup=setup_np)
print("Numpy ", min(t4.repeat(3,1000)))
@ly29
Copy link
Author

ly29 commented Jun 12, 2014

In blender console.

>>> test()
user   0.8943787679891102
Vector 0.2233484220050741
Lambda 0.22744644398335367
Numpy  0.03995985598885454

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment