diff --git a/01_profiling/cpu_profiling/julia1.py b/01_profiling/cpu_profiling/julia1.py index 4cbb7a1..7d89427 100644 --- a/01_profiling/cpu_profiling/julia1.py +++ b/01_profiling/cpu_profiling/julia1.py @@ -1,8 +1,12 @@ """Julia set generator without optional PIL-based image drawing""" +# Intall pillow (Python Imaging Library (Fork)) on python2 or python3 +# https://pypi.python.org/pypi/Pillow/2.5.3 +from __future__ import print_function import time from PIL import Image import array + # area of complex space to investigate x1, x2, y1, y2 = -1.8, 1.8, -1.8, 1.8 c_real, c_imag = -0.62772, -.42193 @@ -13,7 +17,7 @@ def show_greyscale(output_raw, width, height, max_iterations): # convert our output to PIL-compatible input # scale to [0...255] max_iterations = float(max(output_raw)) - print max_iterations + print(max_iterations) scale_factor = float(max_iterations) scaled = [int(o / scale_factor * 255) for o in output_raw] output = array.array('B', scaled) # array of unsigned ints @@ -88,13 +92,13 @@ def calc_pure_python(draw_output, desired_width, max_iterations): zs.append(complex(xcoord, ycoord)) cs.append(complex(c_real, c_imag)) - print "Length of x:", len(x) - print "Total elements:", len(zs) + print("Length of x:", len(x)) + print("Total elements:", len(zs)) start_time = time.time() output = calculate_z_serial_purepython(max_iterations, zs, cs) end_time = time.time() secs = end_time - start_time - print calculate_z_serial_purepython.func_name + " took", secs, "seconds" + print(calculate_z_serial_purepython.__name__ + " took", secs, "seconds") # this sum is expected for 1000^2 grid with 300 iterations assert sum(output) == 33219980 diff --git a/01_profiling/cpu_profiling/julia1_nopil.py b/01_profiling/cpu_profiling/julia1_nopil.py index 2d184c2..93eb14d 100644 --- a/01_profiling/cpu_profiling/julia1_nopil.py +++ b/01_profiling/cpu_profiling/julia1_nopil.py @@ -1,6 +1,8 @@ """Julia set generator without optional PIL-based image drawing""" +from __future__ import print_function import time + # area of complex space to investigate x1, x2, y1, y2 = -1.8, 1.8, -1.8, 1.8 c_real, c_imag = -0.62772, -.42193 @@ -21,7 +23,8 @@ def calculate_z_serial_purepython(maxiter, zs, cs): def calc_pure_python(draw_output, desired_width, max_iterations): - """Create a list of complex co-ordinates (zs) and complex parameters (cs), build Julia set and display""" + """Create a list of complex co-ordinates (zs) and complex parameters (cs), + build Julia set and display""" x_step = (float(x2 - x1) / float(desired_width)) y_step = (float(y1 - y2) / float(desired_width)) x = [] @@ -45,13 +48,13 @@ def calc_pure_python(draw_output, desired_width, max_iterations): zs.append(complex(xcoord, ycoord)) cs.append(complex(c_real, c_imag)) - print "Length of x:", len(x) - print "Total elements:", len(zs) + print ("Length of x:", len(x)) + print ("Total elements:", len(zs)) start_time = time.time() output = calculate_z_serial_purepython(max_iterations, zs, cs) end_time = time.time() secs = end_time - start_time - print calculate_z_serial_purepython.func_name + " took", secs, "seconds" + print (calculate_z_serial_purepython.__name__ + " took", secs, "seconds") # this sum is expected for 1000^2 grid with 300 iterations assert sum(output) == 33219980 diff --git a/01_profiling/decorator_time/julia1_decorator.py b/01_profiling/decorator_time/julia1_decorator.py index 3f3aa3a..01aff06 100644 --- a/01_profiling/decorator_time/julia1_decorator.py +++ b/01_profiling/decorator_time/julia1_decorator.py @@ -1,7 +1,9 @@ """Julia set generator with timing decorator""" +from __future__ import print_function import time from functools import wraps + # area of complex space to investigate x1, x2, y1, y2 = -1.8, 1.8, -1.8, 1.8 c_real, c_imag = -0.62772, -.42193 @@ -13,8 +15,8 @@ def measure_time(*args, **kwargs): t1 = time.time() result = fn(*args, **kwargs) t2 = time.time() - print ( - "@timefn:" + fn.func_name + " took " + str(t2 - t1) + " seconds") + print( + "@timefn:" + fn.__name__ + " took " + str(t2 - t1) + " seconds") return result return measure_time @@ -35,7 +37,8 @@ def calculate_z_serial_purepython(maxiter, zs, cs): def calc_pure_python(draw_output, desired_width, max_iterations): - """Create a list of complex co-ordinates (zs) and complex parameters (cs), build Julia set and display""" + """Create a list of complex co-ordinates (zs) and complex parameters (cs), + build Julia set and display""" x_step = (float(x2 - x1) / float(desired_width)) y_step = (float(y1 - y2) / float(desired_width)) x = [] @@ -59,13 +62,13 @@ def calc_pure_python(draw_output, desired_width, max_iterations): zs.append(complex(xcoord, ycoord)) cs.append(complex(c_real, c_imag)) - print "Length of x:", len(x) - print "Total elements:", len(zs) + print("Length of x:", len(x)) + print("Total elements:", len(zs)) start_time = time.time() output = calculate_z_serial_purepython(max_iterations, zs, cs) end_time = time.time() secs = end_time - start_time - print calculate_z_serial_purepython.func_name + " took", secs, "seconds" + print(calculate_z_serial_purepython.__name__ + " took", secs, "seconds") # this sum is expected for 1000^2 grid with 300 iterations assert sum(output) == 33219980 diff --git a/02_understanding/check_prime.py b/02_understanding/check_prime.py index 6c8d3a1..68e7dfd 100644 --- a/02_understanding/check_prime.py +++ b/02_understanding/check_prime.py @@ -1,14 +1,16 @@ +from __future__ import print_function import math + def check_prime(number): sqrt_number = math.sqrt(number) number_float = float(number) - for i in xrange(2, int(sqrt_number) + 1): + for i in range(2, int(sqrt_number) + 1): if (number_float / i).is_integer(): return False return True if __name__ == "__main__": - print "check_prime(10000000) = ", check_prime(10000000) - print "check_prime(10000019) = ", check_prime(10000019) + print ("check_prime(10000000) = ", check_prime(10000000)) + print ("check_prime(10000019) = ", check_prime(10000019))