本文整理汇总了Python中timeit.repeat函数的典型用法代码示例。如果您正苦于以下问题:Python repeat函数的具体用法?Python repeat怎么用?Python repeat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了repeat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: performance
def performance(name, size, loops=100):
libpath = os.path.join(os.getcwd(), name)
sys.path.append(libpath)
try:
atm
except NameError:
import isa
imp.reload(isa)
times = []
for element in size:
element = int(element)
if element == 1:
time = repeat('atm(0.)', setup='from isa import atm',
number=loops, repeat=3)
elif element > 1:
time = repeat('atm(h)',
setup='from isa import atm\n'
'from numpy import linspace\n'
'h = linspace(0., 11000., {})'
.format(element),
number=loops, repeat=3)
time = 1e3 * min(time) / loops
times.append(time)
sys.path.remove(libpath)
return times
示例2: time_update
def time_update(function, truncate, imsize, picture, input_im, sigma_r, sigma_s, lw, num_thread=None):
#cython parameters
imsize0 = imsize[0]
imsize1 = imsize[1]
output = picture*0.
output5 = np.array(output, np.float32)
input_im5 = np.array(input_im, np.float32)
if num_thread is None:
times = timeit.repeat(lambda: function(sigma_s,
sigma_r,
input_im5,
imsize0,
imsize1,
output5,
lw),
number=3, repeat=5)
else:
times = timeit.repeat(lambda: function(sigma_s,
sigma_r,
input_im5,
imsize0,
imsize1,
output5,
lw,
num_thread),
number=3, repeat=5)
print("{}: {}s".format(str(function) , min(times)))
return min(times)
示例3: time_big_inputs
def time_big_inputs(self):
n_elems_range = np.linspace(1, 2**self.max_exponent, num=self.num_test_points, dtype=int).tolist()
times_merge = []
times_counting = []
sort_case= BigInputsSortCase('merge')
for i,n_elems in enumerate(n_elems_range):
print(i)
sort_case.setup(n_elems=n_elems, max_elem=n_elems)
print('max:' + str(max(sort_case.test_input)))
elapsed_time = min(timeit.repeat(sort_case.sort, number=1, repeat=self.num_repeats))*1000
times_merge.append(elapsed_time)
sort_case = BigInputsSortCase('counting')
for i, n_elems in enumerate(n_elems_range):
print(i)
sort_case.setup(n_elems=n_elems, max_elem=n_elems)
elapsed_time = min(timeit.repeat(sort_case.sort, number=1, repeat=self.num_repeats))*1000
times_counting.append(elapsed_time)
# plot both
plt.plot(n_elems_range, times_merge, color='red', label='Merge sort', linestyle='-', marker='o')
plt.plot(n_elems_range, times_counting, color='blue', label='Counting sort', linestyle='-', marker='o')
plt.title('Big Inputs case')
plt.xlabel('size (length) of the input')
plt.ylabel('ms.')
plt.legend(loc='upper left', frameon=True)
plt.show()
示例4: find_breaking_point
def find_breaking_point(f1, f2, input_generator, start=1, step=1,
limit=1000000, trial_count=1000, repeat_count=3):
"""
Find size of input arguments (n0) for which f2(n0) is faster than f1(n0).
- f1, f2 - functions to test.
- input_generator - function that receives current size of input arguments and returns input data in form of tuple with first item - list of non-keyword arguments and second item - dict of keyword arguments.
- start - initial input data size.
- step - iteration step.
- limit - maximum size of input data.
- trial_count - count of executions of f1/f2 on each iteration.
- repeat_count - to repeat trials several times and use average performance value.
returns n0 - size of input data for which f2(n0) is faster than f1(n0)
or None if reaches limit.
"""
for n in range(start, limit+1):
curr_input = input_generator(n)
# Test first function
f1_results = timeit.repeat(lambda: f1(*curr_input[0], **curr_input[1]),
repeat=repeat_count, number=trial_count)
f1_avg = sum(f1_results) / len(f1_results)
# Test second function
f2_results = timeit.repeat(lambda: f2(*curr_input[0], **curr_input[1]), repeat=repeat_count, number=trial_count)
f2_avg = sum(f2_results) / len(f2_results)
# Compare performance
if f2_avg < f1_avg:
return n
return None
示例5: main
def main():
'''test functions'''
init()
if len(argv) > 1:
n = int(argv[1])
else:
print('nchess.py, usage:\nn print repeat functions')
return
if len(argv) > 2:
print_sol = bool(int(argv[2]))
functions = [perm_all, perm_op1, perm_op2, perm_op3, perm_op4, perm_op5]
if len(argv) > 3:
repeats = int(argv[3])
else:
repeats = 100
if len(argv) > 4:
for func in argv[4:]:
print()
funcstr = str(functions[int(func)]).split(' ')[1]
print(funcstr)
if print_sol:
print(min(timeit.repeat('print({}({}))'.format(funcstr, n),
setup='from __main__ import '+funcstr,
repeat=repeats, number=1)))
else:
print(min(timeit.repeat('{}({})'.format(funcstr, n),
setup='from __main__ import '+funcstr,
repeat=repeats, number=1)))
else:
print(function(n) for function in functions[1:])
示例6: time_already_sorted
def time_already_sorted(self):
max_value_range = np.linspace(1, 2**self.max_exponent, num=self.num_test_points, dtype=int).tolist()
times_diff_merge = []
times_diff_counting = []
for n_elems in max_value_range:
sort_case = AlreadySortedSortCase('merge')
sort_case.setup(n_elems=n_elems)
elapsed_time_sorted = min(timeit.repeat(sort_case.sort, number=1, repeat=self.num_repeats))*1000
sort_case = ManualSortCases('merge')
sort_case.setup(np.random.permutation(n_elems).tolist())
elapsed_time_permuted = min(timeit.repeat(sort_case.sort, number=1, repeat=self.num_repeats))*1000
diff_merge = elapsed_time_permuted - elapsed_time_sorted
print(diff_merge)
times_diff_merge.append(diff_merge)
for n_elems in max_value_range:
sort_case = AlreadySortedSortCase('counting')
sort_case.setup(n_elems=n_elems)
elapsed_time_sorted = min(timeit.repeat(sort_case.sort, number=1, repeat=self.num_repeats))*1000
sort_case = ManualSortCases('counting')
sort_case.setup(np.random.permutation(n_elems).tolist())
elapsed_time_permuted = min(timeit.repeat(sort_case.sort, number=1, repeat=self.num_repeats))*1000
diff_count = elapsed_time_permuted - elapsed_time_sorted
print(diff_count)
times_diff_counting.append(diff_count)
# plot both
plt.plot(max_value_range, times_diff_merge, color='red', label='Merge sort', linestyle='-', marker='o')
plt.plot(max_value_range, times_diff_counting, color='blue', label='Counting sort', linestyle='-', marker='o')
plt.title('Already Sorted case')
plt.xlabel('size (length) of the input')
plt.ylabel(r'$RT_{PERMUTED} - RT_{SORTED}$ [ms.]')
plt.legend(loc='upper left', frameon=True)
plt.show()
示例7: main
def main():
print("Calling on 10: " + str(sum_of_primes(10)))
print("Timing new Prime Method:")
print("Old Method")
print(timeit.repeat("sum_of_primes(2000000)", "from __main__ import sum_of_primes", number =1))
print("New Method")
print(timeit.repeat("better_sum_of_primes(2000000)", "from __main__ import better_sum_of_primes", number =1))
示例8: run_case
def run_case(docs, words, word_range):
SETUP = (
'import random;'
'from {module} import {func} as func;'
'from __main__ import generate_doclist;'
'random.seed("tidovsoctavian");'
'docs_list = generate_doclist({docs}, {words}, {word_range})'
)
octavian = timeit.repeat(
"func(docs_list)",
setup=SETUP.format(
module='set_similarity_octavian',
func='similarity',
docs=docs, words=words, word_range=word_range),
number=NUMBER, repeat=REPEAT)
tido = timeit.repeat(
"func(docs_list)",
setup=SETUP.format(
module='set_similarity_tido',
func='print_similar_docs',
docs=docs, words=words, word_range=word_range),
number=NUMBER, repeat=REPEAT)
return {'octavian': octavian, 'tido': tido}
示例9: main
def main(conf):
"""Run timed benchmark"""
read_sequence = [1, 2, 16, 256, 512, 1024, 2048, 4096, 8192, 16384,
32768, 65536, 262144]
write_sequence = [1, 2, 16, 256, 512, 1024, 2048, 4096, 8192, 16384,
32768, 65536, 262144]
read_results = []
write_results = []
prepare_files(conf)
for i in read_sequence:
read_results.append((i, min(
timeit.repeat("read_mark(%s, filehandle)" % i,
setup = conf['setup_read'], repeat=conf['repeat'],
number=conf['number']))))
for i in write_sequence:
write_results.append((i, min(
timeit.repeat("write_mark(%s, filehandle, data)" % i,
setup = conf['setup_write'], repeat=conf['repeat'],
number=conf['number']))))
out = pprint.PrettyPrinter()
out.pprint(read_results)
out.pprint(write_results)
示例10: time_fit_predict
def time_fit_predict(clf, dfx, dfy, var='TF', num=10, rp=3):
'''time fit and predict with classifier clf on training set dfx, dfy
using num loops and rp repeats'''
# print("time_fit_predict: var", var)
# dfy['TF'] has two states (0, 1)
def fit_clf():
do_fit(clf, dfx, dfy['TF'])
# should run predict on test not train data
def predict_clf():
do_predict(clf, dfx, dfy['TF'])
# dfy['Y'] has six states (1-6)
def fit_clf_multi():
do_fit(clf, dfx, dfy['Y'])
def predict_clf_multi():
do_predict(clf, dfx, dfy['Y'])
if var=='Y':
tfit = min(timeit.repeat(fit_clf_multi, repeat=rp, number=num))
tpred = min(timeit.repeat(predict_clf_multi, repeat=rp, number=num))
else:
tfit = min(timeit.repeat(fit_clf, repeat=rp, number=num))
tpred = min(timeit.repeat(predict_clf, repeat=rp, number=num))
tfit = tfit * 1e3 / num
tpred = tpred * 1e3 / num
return tfit, tpred
示例11: _measure_performance
def _measure_performance():
import timeit
_sqdiff_numba = _make_sqdiff_numba()
print "All times in ms numpy\tnumba"
print "type \tnumpy\tnumba\tC\tspeedup\tspeedup\tsize\talignment"
for _ in range(100):
frame_cropped, template, template_transparent = _random_template()
for l, t in [("template ", template),
("with mask", template_transparent),
("unmasked ", template_transparent[:, :, :3])]:
# pylint: disable=cell-var-from-loop
np_time = min(timeit.repeat(
lambda: _sqdiff_numpy(t, frame_cropped),
repeat=3, number=10)) / 10
c_time = min(timeit.repeat(
lambda: _sqdiff_c(t, frame_cropped),
repeat=3, number=10)) / 10
if _sqdiff_numba:
numba_time = min(timeit.repeat(
lambda: _sqdiff_numba(t, frame_cropped),
repeat=3, number=10)) / 10
else:
numba_time = float('nan')
print "%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%i x %i \t%s" % (
l, np_time * 1000, numba_time * 1000, c_time * 1000,
np_time / c_time, numba_time / c_time,
frame_cropped.shape[1], frame_cropped.shape[0],
frame_cropped.ctypes.data % 8)
示例12: do_it
def do_it(cmd, data_str, num_threads, globals, number, repeat, divisor):
if num_threads == 1:
times = timeit.repeat('%s(%s, core)' % (cmd, data_str), globals=globals, number=number, repeat=repeat)
else:
times = timeit.repeat('_run_x(%s, %s, %s, core=core)' % (cmd, data_str, num_threads), globals=globals,
number=number, repeat=repeat)
print_time(cmd, times, divisor)
示例13: measuringExecutionTimes
def measuringExecutionTimes(self):
print "---- measuringExecutionTimes() ----"
def f(x):
return x * x
import timeit
print timeit.repeat("for x in range(100): lambda x: x*10","",
number=100000)
示例14: main
def main():
for m in maps:
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
tcod.map_set_properties(m, x, y, True, True)
for thread in threads:
thread.start()
print('Python %s\n%s\n%s' % (sys.version, platform.platform(),
platform.processor()))
print('\nComputing field-of-view for %i empty %ix%i maps.' %
(len(maps), MAP_WIDTH, MAP_HEIGHT))
single_time = min(timeit.repeat(test_fov_single, number=1))
print('1 thread: %.2fms' % (single_time * 1000))
multi_time = min(timeit.repeat(test_fov_threads, number=1))
print('%i threads: %.2fms' % (THREADS, multi_time * 1000))
print('%.2f%% efficiency' %
(single_time / (multi_time * THREADS) * 100))
print('\nComputing AStar from corner to corner %i times on seperate empty'
' %ix%i maps.' % (PATH_NUMBER, MAP_WIDTH, MAP_HEIGHT))
single_time = min(timeit.repeat(test_astar_single, number=1))
print('1 thread: %.2fms' % (single_time * 1000))
multi_time = min(timeit.repeat(test_astar_threads, number=1))
print('%i threads: %.2fms' % (THREADS, multi_time * 1000))
print('%.2f%% efficiency' %
(single_time / (multi_time * THREADS) * 100))
示例15: test_performance
def test_performance(self):
call, args = self.get_callable(*self.django_filter_args())
df_time = min(repeat(
lambda: call(*args),
number=self.iterations,
repeat=self.repeat,
))
call, args = self.get_callable(*self.rest_framework_filters_args())
drf_time = min(repeat(
lambda: call(*args),
number=self.iterations,
repeat=self.repeat,
))
diff = (drf_time - df_time) / df_time * 100.0
if verbosity >= 2:
print('\n' + '-' * 32)
print('%s performance' % self.label)
print('django-filter time:\t%.4fs' % df_time)
print('drf-filters time:\t%.4fs' % drf_time)
print('performance diff:\t%+.2f%% ' % diff)
print('-' * 32)
self.assertTrue(drf_time < (df_time * self.threshold))