def _run_with_timing(run, nruns):
"""
Run function `run` and print timing information.
Parameters
----------
run : callable
Any callable object which takes no argument.
nruns : int
Number of times to execute `run`.
"""
twall0 = time.time()
if nruns == 1:
t0 = clock2()
run()
t1 = clock2()
t_usr = t1[0] - t0[0]
t_sys = t1[1] - t0[1]
print("\nIPython CPU timings (estimated):")
print(" User : %10.2f s." % t_usr)
print(" System : %10.2f s." % t_sys)
else:
runs = range(nruns)
t0 = clock2()
for nr in runs:
run()
t1 = clock2()
t_usr = t1[0] - t0[0]
t_sys = t1[1] - t0[1]
print("\nIPython CPU timings (estimated):")
print("Total runs performed:", nruns)
print(" Times : %10s %10s" % ('Total', 'Per run'))
print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
twall1 = time.time()
print("Wall time: %10.2f s." % (twall1 - twall0))
python类run()的实例源码
def capture(self, line, cell):
"""run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
args = magic_arguments.parse_argstring(self.capture, line)
out = not args.no_stdout
err = not args.no_stderr
disp = not args.no_display
with capture_output(out, err, disp) as io:
self.shell.run_cell(cell)
if args.output:
self.shell.user_ns[args.output] = io
def testbench():
plaintext_iter = xrange(plaintext_len_min, plaintext_len_max+plaintext_len_step, plaintext_len_step)
key_len_iter = xrange(key_len_min, key_len_max+key_len_step, key_len_step)
result = [[]]
total_progress = len(plaintext_iter)*len(key_len_iter)*iterations
progress = 0
average_time = 0
time_file = open('testbench_times.txt', 'w')
print "Running Testbench"
start_time = datetime.now()
result.append([0] + list(key_len_iter))
for plaintext_len in plaintext_iter:
same_plaintext_len_results = [plaintext_len]
for key_len in key_len_iter:
single_result = 0
for iteration in xrange(iterations):
start_run_time = time.time()
score = test_run(plaintext_len, key_len)
single_result += score
end_run_time = time.time()
time_file.write(str(end_run_time-start_run_time) + '\n')
average_time += end_run_time - start_run_time
progress += 1
update_progress(progress/float(total_progress+1), status="keylength: %d, textlength: %d " % (key_len, plaintext_len))
same_plaintext_len_results.append(float(single_result)/iterations)
result.append(same_plaintext_len_results)
end_time = datetime.now()
update_progress(1, done='Completed in %s hours ' % str(end_time-start_time).rsplit('.')[0])
print 'average time per break run: %.1f' % (average_time/total_progress)
time_file.close()
with open('testbench.txt', 'w') as file:
file.writelines('\t'.join(str(j) for j in i) + '\n' for i in result)
file.close()
goldhunt_pass4.py 文件源码
项目:Learning-Python-Application-Development
作者: PacktPublishing
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def generate_random_points(ref_radius, total_points):
"""Return x, y coordinate lists representing random points inside a circle.
This function illustrates NumPy capabilities. It is used in the
optimization pass 4, 5, 6 in the chapter on performance of the book
Learning Python Application Development (Packt Publishing).
The run time performance of this function will be
significantly faster compared to the previous optimization pass.
Generates random points inside a circle with center at (0,0). For any point
it randomly picks a radius between 0 and ref_radius.
:param ref_radius: The random point lies between 0 and this radius.
:param total_points: total number of random points to be created
:return: x and y coordinates as lists
.. todo:: Refactor! Move the function to a module like gameutilities.py
"""
# Combination of avoiding the dots (function reevaluations)
# and using local variable. This is similar to the
# optimization pass-3 but here we use equivalent NumPy functions.
l_uniform = np.random.uniform
l_sqrt = np.sqrt
l_pi = np.pi
l_cos = np.cos
l_sin = np.sin
# Note that the variables theta and radius are now NumPy arrays.
theta = l_uniform(0.0, 2.0*l_pi, total_points)
radius = ref_radius*l_sqrt(l_uniform(0.0, 1.0, total_points))
x = radius*l_cos(theta)
y = radius*l_sin(theta)
# x and y thus obtained are NumPy arrays. Return these as Python lists
return x.tolist(), y.tolist()
goldhunt_pass5.py 文件源码
项目:Learning-Python-Application-Development
作者: PacktPublishing
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def generate_random_points(ref_radius, total_points):
"""Return x, y coordinate lists representing random points inside a circle.
This function illustrates NumPy capabilities. It is used in the
optimization pass 4, 5, 6 in the chapter on performance of the book
Learning Python Application Development (Packt Publishing).
The run time performance of this function will be
significantly faster compared to the previous optimization pass.
Generates random points inside a circle with center at (0,0). For any
point, it randomly picks a radius between 0 and ref_radius.
:param ref_radius: The random point lies between 0 and this radius.
:param total_points: total number of random points to be created
:return: x and y coordinates as lists
.. todo:: Refactor! Move the function to a module like gameutilities.py
"""
# Combination of avoiding the dots (function reevaluations)
# and using local variable. This is similar to the
# optimization pass-3 but here we use equivalent NumPy functions.
l_uniform = np.random.uniform
l_sqrt = np.sqrt
l_pi = np.pi
l_cos = np.cos
l_sin = np.sin
# Note that the variables theta and radius are now NumPy arrays.
theta = l_uniform(0.0, 2.0*l_pi, total_points)
radius = ref_radius*l_sqrt(l_uniform(0.0, 1.0, total_points))
x = radius*l_cos(theta)
y = radius*l_sin(theta)
# Unlike optimization pass-4 (which returns x and y as Python lists,
# here it returns the NumPy arrays directly to be consumed by
# the GoldHunt.find_coins method
return x, y
goldhunt_pass4.py 文件源码
项目:Learning-Python-Application-Development
作者: PacktPublishing
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def generate_random_points(ref_radius, total_points):
"""Return x, y coordinate lists representing random points inside a circle.
This function illustrates NumPy capabilities. It is used in the
optimization pass 4, 5, 6 in the chapter on performance of the book
Learning Python Application Development (Packt Publishing).
The run time performance of this function will be
significantly faster compared to the previous optimization pass.
Generates random points inside a circle with center at (0,0). For any point
it randomly picks a radius between 0 and ref_radius.
:param ref_radius: The random point lies between 0 and this radius.
:param total_points: total number of random points to be created
:return: x and y coordinates as lists
.. todo:: Refactor! Move the function to a module like gameutilities.py
"""
# Combination of avoiding the dots (function reevaluations)
# and using local variable. This is similar to the
# optimization pass-3 but here we use equivalent NumPy functions.
l_uniform = np.random.uniform
l_sqrt = np.sqrt
l_pi = np.pi
l_cos = np.cos
l_sin = np.sin
# Note that the variables theta and radius are now NumPy arrays.
theta = l_uniform(0.0, 2.0*l_pi, total_points)
radius = ref_radius*l_sqrt(l_uniform(0.0, 1.0, total_points))
x = radius*l_cos(theta)
y = radius*l_sin(theta)
# x and y thus obtained are NumPy arrays. Return these as Python lists
return x.tolist(), y.tolist()
goldhunt_pass5.py 文件源码
项目:Learning-Python-Application-Development
作者: PacktPublishing
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def generate_random_points(ref_radius, total_points):
"""Return x, y coordinate lists representing random points inside a circle.
This function illustrates NumPy capabilities. It is used in the
optimization pass 4, 5, 6 in the chapter on performance of the book
Learning Python Application Development (Packt Publishing).
The run time performance of this function will be
significantly faster compared to the previous optimization pass.
Generates random points inside a circle with center at (0,0). For any
point, it randomly picks a radius between 0 and ref_radius.
:param ref_radius: The random point lies between 0 and this radius.
:param total_points: total number of random points to be created
:return: x and y coordinates as lists
.. todo:: Refactor! Move the function to a module like gameutilities.py
"""
# Combination of avoiding the dots (function reevaluations)
# and using local variable. This is similar to the
# optimization pass-3 but here we use equivalent NumPy functions.
l_uniform = np.random.uniform
l_sqrt = np.sqrt
l_pi = np.pi
l_cos = np.cos
l_sin = np.sin
# Note that the variables theta and radius are now NumPy arrays.
theta = l_uniform(0.0, 2.0*l_pi, total_points)
radius = ref_radius*l_sqrt(l_uniform(0.0, 1.0, total_points))
x = radius*l_cos(theta)
y = radius*l_sin(theta)
# Unlike optimization pass-4 (which returns x and y as Python lists,
# here it returns the NumPy arrays directly to be consumed by
# the GoldHunt.find_coins method
return x, y
goldhunt_pass6_parallel.py 文件源码
项目:Learning-Python-Application-Development
作者: PacktPublishing
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def generate_random_points(ref_radius, total_points):
"""Return x, y coordinate lists representing random points inside a circle.
This function illustrates NumPy capabilities. It is used in the
optimization pass 4, 5, 6 in the chapter on performance of the book
Learning Python Application Development (Packt Publishing).
The run time performance of this function will be
significantly faster compared to the previous optimization pass.
Generates random points inside a circle with center at (0,0). For any
point, it randomly picks a radius between 0 and ref_radius.
:param ref_radius: The random point lies between 0 and this radius.
:param total_points: total number of random points to be created
:return: x and y coordinates as lists
.. todo:: Refactor! Move the function to a module like gameutilities.py
"""
# Combination of avoiding the dots (function reevaluations)
# and using local variable. This is similar to the
# optimization pass-3 but here we use equivalent NumPy functions.
l_uniform = np.random.uniform
l_sqrt = np.sqrt
l_pi = np.pi
l_cos = np.cos
l_sin = np.sin
# Note that the variables theta and radius are now NumPy arrays.
theta = l_uniform(0.0, 2.0*l_pi, total_points)
radius = ref_radius*l_sqrt(l_uniform(0.0, 1.0, total_points))
x = radius*l_cos(theta)
y = radius*l_sin(theta)
# Unlike optimization pass-4 (which returns x and y as Python lists,
# here it returns the NumPy arrays directly to be consumed by
# the GoldHunt.find_coins method
return x, y
def main():
import cProfile
# cProfile.run("pack_test()")
cProfile.run("pack_test()", "result")
# >python -m cProfile myscript.py -o result
import pstats
p = pstats.Stats("result")
p.strip_dirs().sort_stats(-1).print_stats()
p.strip_dirs().sort_stats("name").print_stats()
p.strip_dirs().sort_stats("cumulative").print_stats(10)
p.sort_stats('tottime', 'cumtime').print_stats(.5, 'pack_test')
def show_gui():
deps_gtk = GuiCommon.requirements_details_gtk()
report_gtk = GuiCommon.get_dependency_report(deps_gtk, prefix="\t")
if GuiCommon.check_dependencies(deps_gtk):
from pycam.Gui.Project import ProjectGui
gui_class = ProjectGui
else:
full_report = []
full_report.append("PyCAM dependency problem")
full_report.append("Error: Failed to load the GTK interface.")
full_report.append("Details:")
full_report.append(report_gtk)
full_report.append("")
full_report.append("Detailed list of requirements: %s" % GuiCommon.REQUIREMENTS_LINK)
log.critical(os.linesep.join(full_report))
return EXIT_CODES["requirements"]
event_manager = get_event_handler()
gui = gui_class(event_manager)
# initialize plugins
plugin_manager = pycam.Plugins.PluginManager(core=event_manager)
plugin_manager.import_plugins()
# some more initialization
gui.reset_preferences()
# TODO: preferences are not loaded until the new format is stable
# self.load_preferences()
# tell the GUI to empty the "undo" queue
gui.clear_undo_states()
event_manager.emit_event("notify-initialization-finished")
# open the GUI
get_mainloop(use_gtk=True).run()
# no error -> return no error code
return None
def _profile_():
try:
import cProfile as profile
except:
import profile
profile.run('_fulltest_()')
#profile.run('_filetest_()')
def __init__(self, shell):
super(ExecutionMagics, self).__init__(shell)
if profile is None:
self.prun = self.profile_missing_notice
# Default execution function used to actually run user code.
self.default_runner = None
def _run_with_timing(run, nruns):
"""
Run function `run` and print timing information.
Parameters
----------
run : callable
Any callable object which takes no argument.
nruns : int
Number of times to execute `run`.
"""
twall0 = time.time()
if nruns == 1:
t0 = clock2()
run()
t1 = clock2()
t_usr = t1[0] - t0[0]
t_sys = t1[1] - t0[1]
print("\nIPython CPU timings (estimated):")
print(" User : %10.2f s." % t_usr)
print(" System : %10.2f s." % t_sys)
else:
runs = range(nruns)
t0 = clock2()
for nr in runs:
run()
t1 = clock2()
t_usr = t1[0] - t0[0]
t_sys = t1[1] - t0[1]
print("\nIPython CPU timings (estimated):")
print("Total runs performed:", nruns)
print(" Times : %10s %10s" % ('Total', 'Per run'))
print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
twall1 = time.time()
print("Wall time: %10.2f s." % (twall1 - twall0))
def capture(self, line, cell):
"""run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
args = magic_arguments.parse_argstring(self.capture, line)
out = not args.no_stdout
err = not args.no_stderr
disp = not args.no_display
with capture_output(out, err, disp) as io:
self.shell.run_cell(cell)
if args.output:
self.shell.user_ns[args.output] = io
def got_protocol(p):
# this needs to be lower than the deferLater in `run`
call_later(1, p.send_ping)
def _run():
run(Config(args.port, args.n, args.t, args.population, args.test, args.value, args.failure, args.tx_rate,
args.fan_out, args.validate, args.ignore_promoter, args.auto_byzantine),
args.broadcast, args.discovery)
def stats_run(npart, nproc, ndim, periodic=False, overwrite=False,
display=False, suppress_final_output=False):
r"""Get timing stats using :package:`cProfile`.
Args:
npart (int): Number of particles.
nproc (int): Number of processors.
ndim (int): Number of dimensions.
periodic (bool, optional): If True, the domain is assumed to be
periodic. Defaults to False.
overwrite (bool, optional): If True, the existing file for this
set of input parameters if overwritten. Defaults to False.
suppress_final_output (bool, optional): If True, the final output
from spawned MPI processes is suppressed. This is mainly for
timing purposes. Defaults to False.
display (bool, optional): If True, display the profile results.
Defaults to False.
"""
perstr = ""
outstr = ""
if periodic:
perstr = "_periodic"
if suppress_final_output:
outstr = "_noout"
fname_stat = 'stat_{}part_{}proc_{}dim{}{}.txt'.format(
npart, nproc, ndim, perstr, outstr)
if overwrite or not os.path.isfile(fname_stat):
cProfile.run(
"from cykdtree.tests import run_test; "+
"run_test({}, {}, nproc={}, ".format(npart, ndim, nproc) +
"periodic={}, ".format(periodic) +
"suppress_final_output={})".format(suppress_final_output),
fname_stat)
if display:
p = pstats.Stats(fname_stat)
p.sort_stats('time').print_stats(10)
return p
return fname_stat
def time_run(npart, nproc, ndim, nrep=1, periodic=False, leafsize=10,
suppress_final_output=False):
r"""Get runing times using :package:`time`.
Args:
npart (int): Number of particles.
nproc (int): Number of processors.
ndim (int): Number of dimensions.
nrep (int, optional): Number of times the run should be performed to
get an average. Defaults to 1.
periodic (bool, optional): If True, the domain is assumed to be
periodic. Defaults to False.
leafsize (int, optional): The maximum number of points that should be
in any leaf in the tree. Defaults to 10.
suppress_final_output (bool, optional): If True, the final output
from spawned MPI processes is suppressed. This is mainly for
timing purposes. Defaults to False.
"""
times = np.empty(nrep, 'float')
for i in range(nrep):
t1 = time.time()
run_test(npart, ndim, nproc=nproc,
periodic=periodic, leafsize=leafsize,
suppress_final_output=suppress_final_output)
t2 = time.time()
times[i] = t2 - t1
return np.mean(times), np.std(times)
def master(options):
"""
Start of the master process.
"""
if not options.silence:
print "Master started on PID %s" % os.getpid()
# start embedded Web server if asked for (this only runs on master)
##
if options.port:
webdir = File(".")
web = Site(webdir)
web.log = lambda _: None # disable annoyingly verbose request logging
reactor.listenTCP(options.port, web)
# we just need some factory like thing .. it won't be used on master anyway
# for actual socket accept
##
factory = Factory()
# create socket, bind and listen ..
port = reactor.listenTCP(options.wsport, factory, backlog=options.backlog)
# .. but immediately stop reading: we only want to accept on workers, not master
port.stopReading()
# fire off background workers
##
for i in range(options.workers):
args = [executable, "-u", __file__, "--fd", str(port.fileno()), "--cpuid", str(i)]
# pass on cmd line args to worker ..
args.extend(sys.argv[1:])
reactor.spawnProcess(
None, executable, args,
childFDs={0: 0, 1: 1, 2: 2, port.fileno(): port.fileno()},
env=os.environ)
reactor.run()
def process(self, buf, eof, tm0):
global filterorder, votewin
# correct back to start of self.ssamples[]
tm0 -= self.ssampleslen / float(self.inrate)
self.ssamples.append(buf)
self.ssampleslen += len(buf)
while True:
if self.ssampleslen < 60 * self.inrate:
break
if eof == False and self.ssampleslen < (votewin+1)*60*self.inrate:
break
samples = numpy.concatenate(self.ssamples)
self.ssamples = None
self.ssampleslen = None
filter = weakutil.butter_bandpass(self.center - self.filterwidth/2,
self.center + self.filterwidth/2,
self.inrate, filterorder)
filtered = scipy.signal.lfilter(filter[0], filter[1], samples)
# down-sampling makes everything run much faster.
# XXX perhaps sacrificing fine alignment?
down = weakutil.resample(filtered, self.inrate, self.lorate)
self.process1(down, tm0)
trim = 60*self.inrate
samples = samples[trim:]
self.ssamples = [ samples ]
self.ssampleslen = len(samples)
tm0 += trim / float(self.inrate)