-
-
Save zeffii/ffd746e1e772d470a932 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
setup_common = """\ | |
from math import sin, cos, radians | |
def sv_zip(*iterables): | |
# zip('ABCD', 'xy') --> Ax By | |
# like standard zip but list instead of tuple | |
sentinel = object() | |
iterators = [iter(it) for it in iterables] | |
while iterators: | |
result = [] | |
for it in iterators: | |
elem = next(it, sentinel) | |
if elem is sentinel: | |
return | |
result.append(elem) | |
yield result | |
U, V, Radius = 40, 50, 3\ | |
""" | |
setup1 = setup_common + """\ | |
def sphere_verts(U, V, Radius): | |
theta = 360/U | |
phi = 180/(V-1) | |
X = [0] | |
Y = [0] | |
Z = [Radius] | |
for i in range(1,V-1): | |
X.extend([round(Radius*cos(radians(theta*j))*sin(radians(phi*i)),8) for j in range(U)]) | |
Y.extend([round(Radius*sin(radians(theta*j))*sin(radians(phi*i)),8) for j in range(U)]) | |
Z.extend([round(Radius*cos(radians(phi*i)),8) for j in range(U)]) | |
X.append(0) | |
Y.append(0) | |
Z.append(-Radius) | |
return list(sv_zip(X,Y,Z)) | |
""" | |
setup2 = setup_common + """\ | |
def sphere_verts2(U, V, Radius): | |
theta = 360/U | |
phi = 180/(V-1) | |
X = [0] | |
Y = [0] | |
Z = [Radius] | |
add_X = X.append | |
add_Y = Y.append | |
extend_Z = Z.extend | |
for i in range(1, V-1): | |
rad_phi_i = radians(phi*i) | |
sin_rad_phi_i = sin(rad_phi_i) | |
cos_rad_phi_i = cos(rad_phi_i) | |
for j in range(U): | |
rad_theta_j = radians(theta*j) | |
add_X(round(Radius * cos(rad_theta_j) * sin_rad_phi_i, 8)) | |
add_Y(round(Radius * sin(rad_theta_j) * sin_rad_phi_i, 8)) | |
extend_Z([round(Radius * cos_rad_phi_i, 8) for j in range(U)]) | |
add_X(0) | |
add_Y(0) | |
Z.append(-Radius) | |
return list(sv_zip(X,Y,Z)) | |
""" | |
setup4 = setup_common + """\ | |
def sphere_verts4(U, V, Radius): | |
theta = 360/U | |
phi = 180/(V-1) | |
X = [0] | |
Y = [0] | |
Z = [Radius] | |
add_X = X.append | |
add_Y = Y.append | |
extend_Z = Z.extend | |
for i in range(1, V-1): | |
rad_phi_i = radians(phi*i) | |
sin_rad_phi_i = sin(rad_phi_i) | |
cos_rad_phi_i = cos(rad_phi_i) | |
for j in range(U): | |
rad_theta_j = radians(theta*j) | |
add_X(Radius * cos(rad_theta_j) * sin_rad_phi_i) | |
add_Y(Radius * sin(rad_theta_j) * sin_rad_phi_i) | |
extend_Z([Radius * cos_rad_phi_i for j in range(U)]) | |
add_X(0) | |
add_Y(0) | |
Z.append(-Radius) | |
return list(sv_zip(X,Y,Z)) | |
""" | |
from timeit import Timer | |
# first argument is the code to be run, the second "setup" argument is only run once, | |
# and it not included in the execution time. | |
# t = Timer("""m = sphere_verts(U, V, Radius)""", setup=setup1) # 6.5 on 100 | |
# print(t.timeit(100)) # repeat 100 times / 64.64094696392976 | |
t = Timer("""m = sphere_verts2(U, V, Radius)""", setup=setup2) # 5.3 on 100 | |
print(t.timeit(100)) # repeat 100 times / 53.44165117354301 | |
t = Timer("""m = sphere_verts4(U, V, Radius)""", setup=setup4) # 5.3 on 100 | |
print(t.timeit(100)) # repeat 100 times / 53.44165117354301 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment