Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed func_name with __name__ #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions 01_profiling/cpu_profiling/julia1.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions 01_profiling/cpu_profiling/julia1_nopil.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = []
Expand All @@ -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
Expand Down
15 changes: 9 additions & 6 deletions 01_profiling/decorator_time/julia1_decorator.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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 = []
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions 02_understanding/check_prime.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is explicitly xrange since our main target is python2.7.

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))