Python 断言一次运行明显快于另一次运行
我需要做一个函数测试,断言一次运行比另一次运行快得多 以下是我迄今为止编写的代码:Python 断言一次运行明显快于另一次运行,python,unit-testing,testing,time,Python,Unit Testing,Testing,Time,我需要做一个函数测试,断言一次运行比另一次运行快得多 以下是我迄今为止编写的代码: def test_run5(self): cmd_line = ["python", self.__right_def_file_only_files] start = time.clock() with self.assertRaises(SystemExit): ClassName().run(cmd_line) end = time.clock() r
def test_run5(self):
cmd_line = ["python", self.__right_def_file_only_files]
start = time.clock()
with self.assertRaises(SystemExit):
ClassName().run(cmd_line)
end = time.clock()
runtime1 = end - start
start = time.clock()
with self.assertRaises(SystemExit):
ClassName().run(cmd_line)
end = time.clock()
runtime2 = end - start
self.assertTrue(runtime2 < runtime1 * 1.4)
def测试_运行5(自):
cmd_line=[“python”,self.\uuuuu right\u def\u file\u only\u files]
开始=时间。时钟()
使用self.assertRaises(SystemExit):
ClassName().run(命令行)
end=time.clock()
runtime1=结束-开始
开始=时间。时钟()
使用self.assertRaises(SystemExit):
ClassName().run(命令行)
end=time.clock()
runtime2=结束-开始
self.assertTrue(runtime2
它可以工作,但我不喜欢这种方式,因为1.4因子是在我的具体执行示例中实验选择的
如何测试第二次执行是否总是比第一次快
编辑
我不认为有必要对其进行解释,但在我的程序环境中,我不能说某个因素对未知的执行非常重要
整个程序是一种Make
,管道定义文件将定义什么是“速度的显著差异”,而不是我:
- 如果定义文件包含许多非常快的规则,那么两次连续执行之间的执行时间差将非常小,比如说快5%,但仍然非常显著
- 否则,如果定义文件包含很少的规则,但包含很长的规则,那么差异会很大,比方说快90%,因此5%的差异根本不重要
def get_best_factor(full_exec_time, rule_count, maximum_ratio=1):
average_rule_time = full_exec_time / rule_count
return 1 + (maximum_ratio * average_rule_time / (1.5 + average_rule_time))
参数是full_exec_time
,它是给定管道定义文件的最大执行时间runtime1
是给定管道定义文件中的规则数rule\u count
意味着第二次执行将比第一次快100%(实际上不可能)maximum_ratio
Michaelis-Menten动力学方程的可变参数是平均规则执行时间。我任意选择1.5秒
作为平均规则执行时间,执行时间应该比最大/2
快。它是实际参数,取决于您对该方程的使用。为什么1.4
?仅使用runtime2
还可以检查runtime2
是否更快!您也可以这样做,因为runtime2
没有回答“显著”这一点。这两次执行的运行时大致相同,运行时runtime2
实际上低于runtime1
。感谢assertGreater
,我将使用它。这取决于您定义多少是重要的。我们不能为您这样做。我在想,也许有一个公式可以动态地找到runtime1
的好因素。