def perf_comp_data(func_list, data_list, rep=3, number=1):
'''Function to compare the performance of different functions.
Parameters
func_list : list
list with function names as strings
data_list : list
list with data set names as strings
rep : int
number of repetitions of the whole comparison
number : int
number ofexecutions for every function
'''
from timeit import repeat
res_list = {}
for name in enumerate(func_list):
stmt = name[1] + '(' + data_list[name[0]] + ')'
setup = "from __main__ import " + name[1] + ','+ data_list[name[0]]
results = repeat(stmt=stmt, setup=setup, repeat=rep, number=number)
res_list[name[1]] = sum(results) / rep
res_sort = sorted(res_list.items(), key = lambda item : item[1])
for item in res_sort:
rel = item[1] / res_sort[0][1]
print ('function: ' + item[0] + ', av. time sec: %9.5f, ' % item[1] + 'relative: %6.1f' % rel)
from math import * def f(x): return abs(cos(x)) ** 0.5 + sin(2 + 3 * x)
i=500000 a_py = range(i)
def f1(a):
res = []
for x in a:
res.append(f(x))
return res
def f2(a): return [f(x) for x in a]
def f3(a): ex = 'abs(cos(x)) **0.5+ sin(2 + 3 * x)' return [eval(ex) for x in a]
def f7(a): return (f(x) for x in a)
def f8(a): return map(f, a)
import numpy as np a_np = np.arange(i) def f4(a): return (np.abs(np.cos(a)) ** 0.5 + np.sin(2 + 3 * a)) import numexpr as ne def f5(a): ex = 'abs(cos(a)) ** 0.5 + sin( 2 + 3 * a)' ne.set_num_threads(1) return ne.evaluate(ex) def f6(a): ex = 'abs(cos(a)) ** 0.5 + sin(2 + 3 * a)' ne.set_num_threads(2) return ne.evaluate(ex)
func_list = ['f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8'] data_list = ['a_py', 'a_py', 'a_py', 'a_np', 'a_np', 'a_np', 'a_py', 'a_py'] perf_comp_data(func_list, data_list)
function: f8, av. time sec: 0.00000, relative: 1.0 function: f7, av. time sec: 0.00001, relative: 1.7 function: f6, av. time sec: 0.03787, relative: 11982.7 function: f5, av. time sec: 0.05838, relative: 18472.4 function: f4, av. time sec: 0.09711, relative: 30726.8 function: f2, av. time sec: 0.82343, relative: 260537.0 function: f1, av. time sec: 0.92557, relative: 292855.2 function: f3, av. time sec: 32.80889, relative: 10380938.6
function: f8, av. time sec: 0.000002483, relative: 1.0 function: f7, av. time sec: 0.000004741, relative: 1.9 function: f5, av. time sec: 0.028068110, relative: 11303.0 function: f6, av. time sec: 0.031389788, relative: 12640.6 function: f4, av. time sec: 0.053619114, relative: 21592.4 function: f1, av. time sec: 0.852619225, relative: 343348.7 function: f2, av. time sec: 1.009691877, relative: 406601.7 function: f3, av. time sec: 26.035869787, relative: 10484613.6
x = np.random.standard_normal(( 3, 1500000)) c = np.array(x, order='C') f = np.array(x, order='F')
%timeit c.sum(axis=0) %timeit c.std(axis=0) %timeit f.sum(axis=0) %timeit f.std(axis=0) %timeit c.sum(axis=1) %timeit c.std(axis=1) %timeit f.sum(axis=1) %timeit f.std(axis=1)
loops, best of 3: 12.1 ms per loop loops, best of 3: 83.3 ms per loop loops, best of 3: 70.2 ms per loop loop, best of 3: 235 ms per loop loops, best of 3: 7.11 ms per loop loops, best of 3: 37.2 ms per loop loops, best of 3: 54.7 ms per loop loops, best of 3: 193 ms per loop
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有