| 1 |
import sys |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
try: |
| 6 |
|
| 7 |
import __builtin__ as builtins |
| 8 |
except ImportError: |
| 9 |
|
| 10 |
import builtins |
| 11 |
|
| 12 |
try: |
| 13 |
builtins.profile |
| 14 |
except AttributeError: |
| 15 |
|
| 16 |
def profile(func): |
| 17 |
return func |
| 18 |
|
| 19 |
builtins.profile = profile |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
"""Julia set generator without optional PIL-based image drawing""" |
| 26 |
import time |
| 27 |
|
| 28 |
|
| 29 |
x1, x2, y1, y2 = -1.8, 1.8, -1.8, 1.8 |
| 30 |
c_real, c_imag = -0.62772, -0.42193 |
| 31 |
|
| 32 |
|
| 33 |
@profile |
| 34 |
def calculate_z_serial_purepython(maxiter, zs, cs): |
| 35 |
"""Calculate output list using Julia update rule""" |
| 36 |
output = [0] * len(zs) |
| 37 |
for i in range(len(zs)): |
| 38 |
n = 0 |
| 39 |
z = zs[i] |
| 40 |
c = cs[i] |
| 41 |
while abs(z) < 2 and n < maxiter: |
| 42 |
z = z * z + c |
| 43 |
n += 1 |
| 44 |
output[i] = n |
| 45 |
return output |
| 46 |
|
| 47 |
|
| 48 |
@profile |
| 49 |
def calc_pure_python(desired_width, max_iterations): |
| 50 |
"""Create a list of complex coordinates (zs) and complex |
| 51 |
parameters (cs), build Julia set, and display""" |
| 52 |
x_step = float(x2 - x1) / float(desired_width) |
| 53 |
y_step = float(y1 - y2) / float(desired_width) |
| 54 |
x = [] |
| 55 |
y = [] |
| 56 |
ycoord = y2 |
| 57 |
while ycoord > y1: |
| 58 |
y.append(ycoord) |
| 59 |
ycoord += y_step |
| 60 |
xcoord = x1 |
| 61 |
while xcoord < x2: |
| 62 |
x.append(xcoord) |
| 63 |
xcoord += x_step |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
zs = [] |
| 69 |
cs = [] |
| 70 |
for ycoord in y: |
| 71 |
for xcoord in x: |
| 72 |
zs.append(complex(xcoord, ycoord)) |
| 73 |
cs.append(complex(c_real, c_imag)) |
| 74 |
print("Length of x:", len(x)) |
| 75 |
print("Total elements:", len(zs)) |
| 76 |
start_time = time.process_time() |
| 77 |
output = calculate_z_serial_purepython(max_iterations, zs, cs) |
| 78 |
end_time = time.process_time() |
| 79 |
secs = end_time - start_time |
| 80 |
sys.stdout.flush() |
| 81 |
sys.stderr.flush() |
| 82 |
output_str = "calculate_z_serial_purepython took " + str(secs) + " seconds" |
| 83 |
print(output_str, file=sys.stderr) |
| 84 |
sys.stderr.flush() |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if __name__ == "__main__": |
| 93 |
|
| 94 |
|
| 95 |
calc_pure_python(desired_width=1000, max_iterations=300) |
| 96 |
sys.exit(-1) |