zoukankan      html  css  js  c++  java
  • 【python】时间性能分析

    参考:

    http://blog.jobbole.com/47619/

    http://chenpeng.info/html/1754

    1.计算整个程序运行时间,直接用linux的time命令即可

    time python progfrom.py

    2.显示每一行程序运行的时间

    用line_profiler

    安装:

    sudo apt-get install python-dev
    pip install line_profiler

    第一行不是必须的,但是有的时候直接写第二行会报错。

    安装好后,会有提示,告诉你在哪个路径下生成了kernprof文件,我的是在/home/myname/.local/bin/kernprof

    在想测量的函数上使用@profiler装饰器。不需要import任何东西
    Primes.py

    @profile
    def primes(n):
        if n==2:
            return [2]
        elif n<2:
            return []
        s=range(3,n+1,2)
        mroot = n ** 0.5
        half=(n+1)/2-1
        i=0
        m=3
        while m <= mroot:
            if s[i]:
                j=(m*m-3)/2
                s[j]=0
                while j<half:
                    s[j]=0
                    j+=m
            i=i+1
            m=2*i+3
        return [2]+[x for x in s if x]
    primes(100)
     
    .
     

    运行:

    /home/myname/.local/bin/kernprof -l -v Primes.py

    前面的路径就是kernprof的路径

    可以通过 -help选项查看帮助

    结果:

     
    Wrote profile results to primes.py.lprof
    Timer unit: 1e-06 s
     
    File: primes.py
    Function: primes at line 2
    Total time: 0.00019 s
     
    Line #      Hits         Time  Per Hit   % Time  Line Contents
    ==============================================================
         2                                           @profile
         3                                           def primes(n):
         4         1            2      2.0      1.1      if n==2:
         5                                                   return [2]
         6         1            1      1.0      0.5      elif n<2:
         7                                                   return []
         8         1            4      4.0      2.1      s=range(3,n+1,2)
         9         1           10     10.0      5.3      mroot = n ** 0.5
        10         1            2      2.0      1.1      half=(n+1)/2-1
        11         1            1      1.0      0.5      i=0
        12         1            1      1.0      0.5      m=3
        13         5            7      1.4      3.7      while m <= mroot:
        14         4            4      1.0      2.1          if s[i]:
        15         3            4      1.3      2.1              j=(m*m-3)/2
        16         3            4      1.3      2.1              s[j]=0
        17        31           31      1.0     16.3              while j<half:
        18        28           28      1.0     14.7                  s[j]=0
        19        28           29      1.0     15.3                  j+=m
        20         4            4      1.0      2.1          i=i+1
        21         4            4      1.0      2.1          m=2*i+3
        22        50           54      1.1     28.4      return [2]+[x for x in s if x]
     
    .
     

    在里面寻找花费时间比较长的行,有些地方在优化之后能带来极大的改进。

  • 相关阅读:
    选择高性能NoSQL数据库的5个步骤
    如何将 Redis 用于微服务通信的事件存储
    让你的AI模型尽可能的靠近数据源
    Collections.sort 给集合排序
    Bootstrap 文件上传插件File Input的使用
    select2 api参数的文档
    textarea 标签换行及靠左
    JSON
    JDK的get请求方式
    通讯录作业
  • 原文地址:https://www.cnblogs.com/dplearning/p/5151849.html
Copyright © 2011-2022 走看看