Здесь рассматриваются методики профилировали в Питоне.
Это скорее памятка самому себе, но если кому-то пригодится, буду рад!)
Так же доступна интерактивная версия Ipython Notebook.
http://pynash.org/2013/03/06/timing-and-profiling.html — По теме
https://ru.wikipedia.org/wiki/Множество_Жюлиа —
Рассматриваемый пример
# Перед этим pip install line_profiler, memory_profiler
%load_ext line_profiler
%load_ext memory_profilerfrom functools import wrapsdef timefn(fn):
@wraps(fn)
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»)
return result
return measure_time
#@timefn
#@profile
def calculate_z_serial_purepython(maxiter, zs, cs):
"""Calculate output list using Julia update rule"""output = [0] * len(zs)
for i in xrange(len(zs)):
n=0
z = zs[i]
c = cs[i]
while abs(z) < 2 and n < maxiter:
z=z*z+c
n+=1
output[i] = n
return output
import timex1, x2, y1, y2 = —1.8, 1.8, —1.8, 1.8
c_real, c_imag = —0.62772, -.42193
def calc_pure_python(desired_width</sp an>, max_iterations):
«»»Create a list of complex coordinates (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=[]
y=[]
ycoord = y2
while ycoord > y1:
y.append(ycoord)
ycoord += y_step
xcoord = x1
while xcoord < x2:
x.append(xcoord)
xcoord += x_step
##################### SHOW PYTHON HEAP
from guppy import hpy;
hp = hpy()
print «heapy after creating y and x lists of floats»
h = hp.heap()
print h
######################
# Build a list of coordinates and the initial condition for each cell.
# Note that our initial condition is a constant and could easily be removed; # we use it to simulate a real-world scenario with several inputs to
# our function.
zs=[]
cs=[]
for ycoord in y:
for xcoord in x:
zs.append(complex(xcoord, ycoord))
cs.append(complex(c_real, c_imag))
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»
# This sum is expected for a 1000^2 grid with 300 iterations. # It catches minor errors we might introduce when we’re
# working on a fixed set of inputs.
#assert sum(output) == 33219980
return output
"""
При построении с учетом отттенков серого, цвет меняющийся высоким контрастом дал бы нам представление о том,
где стоимость функции медленно меняется, а где быстро. На данном
