-
-
Save ly29/c2b714b91b068ac9d914 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In blender console.