Skip to main content

Python 基准测试/基准测试框架

项目描述

benchmark是一个 Python 基准测试框架,在基本结构上类似于 Steve Purcell 的 unittest 。它很简单:

import benchmark

import math

class Benchmark_Sqrt(benchmark.Benchmark):

    each = 100 # allows for differing number of runs

    def setUp(self):
        # Only using setUp in order to subclass later
        # Can also specify tearDown, eachSetUp, and eachTearDown
        self.size = 25000

    def test_pow_operator(self):
        for i in xrange(self.size):
            z = i**.5

    def test_pow_function(self):
        for i in xrange(self.size):
            z = pow(i, .5)

    def test_sqrt_function(self):
        for i in xrange(self.size):
            z = math.sqrt(i)

class Benchmark_Sqrt2(Benchmark_Sqrt):
    # Subclass the previous benchmark to change input using
    # self.setUp()

    label = "Benchmark Sqrt on a larger range"
    # The above label comes from the class name, this oen
    # comes from the label attribute

    each = 50

    def setUp(self):
        self.size = 750000

if __name__ == '__main__':
    benchmark.main(format="markdown", numberFormat="%.4g")
    # could have written benchmark.main(each=50) if the
    # first class shouldn't have been run 100 times.

产生:

Benchmark Report
================

Benchmark Sqrt
--------------

         name | rank | runs |     mean |        sd | timesBaseline
--------------|------|------|----------|-----------|--------------
 pow operator |    1 |  100 | 0.004163 | 0.0001469 |           1.0
sqrt function |    2 |  100 | 0.004584 |  0.000379 | 1.10126499627
 pow function |    3 |  100 | 0.005496 | 0.0002709 | 1.32031909568

Benchmark Sqrt on a larger range
--------------------------------

         name | rank | runs |   mean |       sd | timesBaseline
--------------|------|------|--------|----------|--------------
 pow operator |    1 |   50 | 0.1265 | 0.004772 |           1.0
sqrt function |    2 |   50 | 0.1375 | 0.004729 |    1.08705384
 pow function |    3 |   50 | 0.1649 | 0.003935 | 1.30357975885

Each of the above 450 runs were run in random, non-consecutive order by
`benchmark` v0.1.4 (http://jspi.es/benchmark) with Python 2.7.1
Darwin-11.3.0-x86_64 on 2012-04-17 19:43:01.

示例文件夹中提供了更多示例,或访问 http://jspi.es/benchmark了解更多信息。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

benchmark-0.1.5.tar.gz (7.1 kB 查看哈希)

已上传 source