Debian Codegraph @

benchmarks/julia1_nopil.py

raw symbols SCIP
1 import sys
2
3 # Disable the @profile decorator if none has been declared.
4
5 try:
6 # Python 2
7 import __builtin__ as builtins
8 except ImportError:
9 # Python 3
10 import builtins
11
12 try:
13 builtins.profile
14 except AttributeError:
15 # No line profiler, provide a pass-through version
16 def profile(func):
17 return func
18
19 builtins.profile = profile
20
21
22 # Pasted from Chapter 2, High Performance Python - O'Reilly Media;
23 # minor modifications for Python 3 by Emery Berger
24
25 """Julia set generator without optional PIL-based image drawing"""
26 import time
27
28 # area of complex space to investigate
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 # Build a list of coordinates and the initial condition for each cell.
65 # Note that our initial condition is a constant and could easily be removed;
66 # we use it to simulate a real-world scenario with several inputs to
67 # our function.
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 # This sum is expected for a 1000^2 grid with 300 iterations.
87 # It catches minor errors we might introduce when we're
88 # working on a fixed set of inputs.
89 ### assert sum(output) == 33219980
90
91
92 if __name__ == "__main__":
93 # Calculate the Julia set using a pure Python solution with
94 # reasonable defaults for a laptop
95 calc_pure_python(desired_width=1000, max_iterations=300)
96 sys.exit(-1) # To force output from py-spy