美文网首页
python3 评估程序性能,复杂度,timeit, profi

python3 评估程序性能,复杂度,timeit, profi

作者: LeeMin_Z | 来源:发表于2018-07-26 23:13 被阅读247次

1. 评估复杂度

手动估算代码行数和计算复杂度。
迭代次数 * 循环中代码行数 * 一行的复杂度 O(n)
复杂度一般看级数, O(n), O(n^2)

2. 用ipython的timeit 算出平均运行时间

#uses ipynb

import time
strings = ['foo','foobar','bax','ssss','python'] * 10000

%timeit [x for x in strings if x[:3] == 'foo']
%timeit [x for x in strings if x.startswith('foo')]
#output 

5.79 ms ± 316 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
9.7 ms ± 520 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

3. python3的Profile分析性能

  1. 分析某个函数性能%prun add_and_sum(x,y)

%prun(cProfile)做宏观的性能分析,lprun做微观每一行的分析。

#uses ipython

In [1]: from numpy.random import randn
   ...:
   ...: def add_and_sum(x,y):
   ...:     added = x + y
   ...:     summed = added.sum(axis=1)
   ...:     return summed
   ...:
   ...: def call_fun():
   ...:     x = randn(1000,1000)
   ...:     y = randn(1000,1000)
   ...:     return add_and_sum(x,y)
   ...:

In [14]: %prun add_and_sum(x,y)
         7 function calls in 0.075 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.052    0.052    0.067    0.067 <ipython-input-1-c92741b0d5ac>:3(a
dd_and_sum)
        1    0.015    0.015    0.015    0.015 {method 'reduce' of 'numpy.ufunc'
objects}
        1    0.008    0.008    0.075    0.075 <string>:1(<module>)
        1    0.000    0.000    0.075    0.075 {built-in method builtins.exec}
        1    0.000    0.000    0.015    0.015 {method 'sum' of 'numpy.ndarray' o
bjects}
        1    0.000    0.000    0.015    0.015 _methods.py:31(_sum)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Prof
iler' objects}

  1. 常用语不想看那个模块的源码,简单测试/比较性能
# IN[1]
import cProfile
import re
cProfile.run('re.compile("foo|bar")')
#OUT[1]
 5 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 re.py:231(compile)
        1    0.000    0.000    0.000    0.000 re.py:286(_compile)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
#test code Python3.6 
import numpy as np
from numpy.linalg import eigvals

def run_experiment(niter = 100):
    K =100
    results = []
    for _ in xrange(niter):
        mat = np.random.rand(K,K)
        max_eigenvalue = np.abs(eigvals(mat)).max()
        return results
    some_results = run_experiment()
    print('largest one %s'% np.max(some_results))

4. 建议的代码格式

扁平结构比嵌套结构好。编写函数和类时注意低耦合和结构化,容易测试和调用。


参考

  1. 《利用python进行数据分析》
  2. 官网 27.4. The Python Profilers
  3. https://pymotw.com/3/profile/

2018.5.18 目前搞不出来的,一直到2018.7.26也并没有想法的问题...............以后想到再回来。

In [19]: %run -p -s cumulative n2.py
ERROR:root:Line magic function `%lprun` not found.

写作时间
2018.5.18 草稿
2018.7.26 V1.0

相关文章

  • python3 评估程序性能,复杂度,timeit, profi

    1. 评估复杂度 手动估算代码行数和计算复杂度。迭代次数 * 循环中代码行数 * 一行的复杂度 O(n)复杂度一般...

  • 时间复杂度和基本数据结构

    一、基本概念 时间复杂度:评估执行程序所需的时间。可以估算出程序对处理器的使用程度。空间复杂度:评估执行程序所需的...

  • Python算法评估

    时间评估 timeit模块是为执行相对可靠的计时操作设计的。(Linux同样有timeit命令) 例如: 如果想要...

  • 数据结构与算法(二)时间复杂度和空间复杂度

    算法效率 算法的效率主要由以下两个复杂度来评估: 时间复杂度(time complexity):评估执行程序所需的...

  • 如何评判算法好坏

    算法好坏可以通过下面两个纬度进行评判: 时间复杂度评估执行程序的次数(执行时间) 空间复杂度评估执行程序所需的存储...

  • 算法笔记 —— 复杂度

    算法的效率 算法的效率主要由以下两个复杂度来评估: 时间复杂度:评估执行程序所需的时间。可以估算出程序对处理器的使...

  • Linux性能优化实战 —— CPU

    Linux性能分析概要 1. 性能指标 选择指标评估应用程序和系统的性能 为应用程序和系统设置性能目标 进行性能基...

  • iOS-性能优化深入探究

    iOS-性能优化深入探究 上图是几种时间复杂度的关系,性能优化一定程度上是为了降低程序执行效率减低时间复杂度。如下...

  • Instruments

    iOS性能优化:Instruments工具 打开:Product - > Profile 1、Time Profi...

  • Python Jupyter Notebook 中的代码性能评测

    在 notebooke 中执行 %timeit 可以输出某个函数代码的性能测评结果: 在 notebooke 中执...

网友评论

      本文标题:python3 评估程序性能,复杂度,timeit, profi

      本文链接:https://www.haomeiwen.com/subject/nlsmdftx.html