test_irr.py 文件源码

python
阅读 37 收藏 0 点赞 0 评论 0

项目:irr 作者: better 项目源码 文件源码
def test_performance():
    us_times = []
    np_times = []
    ns = [10, 20, 50, 100]
    for n in ns:
        k = 100
        sums = [0.0, 0.0]
        for j in range(k):
            r = math.exp(random.gauss(0, 1.0 / n)) - 1
            x = random.gauss(0, 1)
            d = [x] + [0.0] * (n-2) + [-x * (1+r)**(n-1)]

            results = []
            for i, f in enumerate([irr.irr, numpy.irr]):
                t0 = time.time()
                results.append(f(d))
                sums[i] += time.time() - t0

            if not numpy.isnan(results[1]):
                assert results[0] == pytest.approx(results[1])
        for times, sum in zip([us_times, np_times], sums):
            times.append(sum/k)

    try:
        from matplotlib import pyplot
        import seaborn
    except ImportError:
        return

    pyplot.plot(ns, us_times, label='Our library')
    pyplot.plot(ns, np_times, label='Numpy')
    pyplot.xlabel('n')
    pyplot.ylabel('time(s)')
    pyplot.yscale('log')
    pyplot.savefig('plot.png')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号