Skip to content

Instantly share code, notes, and snippets.

@ly29
Created June 12, 2014 09:08
Show Gist options
  • Save ly29/64ef0392acecbb49d0a2 to your computer and use it in GitHub Desktop.
Save ly29/64ef0392acecbb49d0a2 to your computer and use it in GitHub Desktop.
numpy 2, item wise
from timeit import Timer
setup_user = """
from math import sqrt
a= [10,11,12]
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= [10,11,12]
def len_vec(v):
return Vector(v).length
"""
setup_lambda = """
from mathutils import Vector
a= [10,11,12]
len_lambda=lambda a:Vector(a).length
"""
setup_np = """
from numpy.linalg import norm
a = [10,11,12]
def len_np(v):
return norm(v)
"""
def test():
t1 = Timer("""m = len_user(a)""", setup=setup_user)
print("user ", min(t1.repeat(3,100000)))
t2 = Timer("""m = len_vec(a)""", setup=setup_vector)
print("Vector", min(t2.repeat(3,100000)))
t3 = Timer("""m= len_lambda(a)""", setup=setup_lambda)
print("Lambda", min(t3.repeat(3,100000)))
t4 = Timer("""m = len_np(a)""", setup=setup_np)
print("Numpy ", min(t4.repeat(3,100000)))
@ly29
Copy link
Author

ly29 commented Jun 12, 2014

blender console

>>> test()
user   0.0768065839947667
Vector 0.06031997298123315
Lambda 0.06056222500046715
Numpy  2.192653567995876

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