推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
jamiesun
V2EX  ›  Python

import time;start =time.time();print time.time()-start = 8.60691070557e-05

  •  
  •   jamiesun ·
    jamiesun · Feb 12, 2015 · 3583 views
    This topic created in 4185 days ago, the information mentioned may be changed or developed.
    10 replies    2015-02-14 15:47:57 +08:00
    jamiesun
        1
    jamiesun  
    OP
       Feb 12, 2015
    隐式转换的坑,用 "%.6f"%(time.time()-end)
    binux
        2
    binux  
       Feb 12, 2015
    你说什么?
    zhyu
        3
    zhyu  
       Feb 12, 2015
    不懂 lz 想表达什么
    有求时间差的需求,不用 datetime.timedelta 么
    fortunezhang
        4
    fortunezhang  
       Feb 12, 2015
    import time
    start = time.clock()
    do something...
    end = time.clock()
    print(end-start)
    jamiesun
        5
    jamiesun  
    OP
       Feb 12, 2015   ❤️ 1
    @fortunezhang

    这个不行,不是clock与time的问题,而是end-start被自动转换为科学计数,在函数执行几乎无间隔的情况下发生,比如函数直接return,这种情况就会出现了个很大的数字。

    所以用"%.6f"%(time.time()-end) 转换下就好了

    @zhyu 我是计算函数执行时间,timedelta不适用。
    hahastudio
        6
    hahastudio  
       Feb 12, 2015
    1. 性能分析有 timeit 和 profile
    2. python 里 call function 是一个很有代价的操作
    fortunezhang
        7
    fortunezhang  
       Feb 13, 2015
    @jamiesun 如果你相求函数或者其他程序运行时间,这个就可以,我一般用这个。否则,可能不行。
    zhyu
        8
    zhyu  
       Feb 13, 2015
    @jamiesun 计算函数执行时间用 timeit
    jamiesun
        9
    jamiesun  
    OP
       Feb 13, 2015
    @zhyu 但是我是在web运行期间执行,应用有状态
    jamiesun
        10
    jamiesun  
    OP
       Feb 14, 2015
    谢谢各位,最后还是回到profile
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:00 · PVG 05:00 · LAX 14:00 · JFK 17:00
    ♥ Do have faith in what you're doing.