Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 断言一次运行明显快于另一次运行_Python_Unit Testing_Testing_Time - Fatal编程技术网

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
    是给定管道定义文件中的规则数
  • maximum_ratio
    意味着第二次执行将比第一次快100%(实际上不可能)

Michaelis-Menten动力学方程的可变参数是平均规则执行时间。我任意选择
1.5秒
作为平均规则执行时间,执行时间应该比
最大/2
快。它是实际参数,取决于您对该方程的使用。

为什么
1.4
?仅使用
runtime2
还可以检查
runtime2
是否更快!您也可以这样做,因为
runtime2
没有回答“显著”这一点。这两次执行的运行时大致相同,运行时
runtime2
实际上低于
runtime1
。感谢
assertGreater
,我将使用它。这取决于您定义多少是重要的。我们不能为您这样做。我在想,也许有一个公式可以动态地找到
runtime1
的好因素。