V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2015-02-12 16:24:10 +08:00 · 2903 次点击
    这是一个创建于 3374 天前的主题,其中的信息可能已经有所发展或是发生改变。
    10 条回复    2015-02-14 15:47:57 +08:00
    jamiesun
        1
    jamiesun  
    OP
       2015-02-12 16:56:17 +08:00
    隐式转换的坑,用 "%.6f"%(time.time()-end)
    binux
        2
    binux  
       2015-02-12 17:06:59 +08:00
    你说什么?
    zhyu
        3
    zhyu  
       2015-02-12 17:11:49 +08:00
    不懂 lz 想表达什么
    有求时间差的需求,不用 datetime.timedelta 么
    fortunezhang
        4
    fortunezhang  
       2015-02-12 19:48:24 +08:00
    import time
    start = time.clock()
    do something...
    end = time.clock()
    print(end-start)
    jamiesun
        5
    jamiesun  
    OP
       2015-02-12 22:54:21 +08:00   ❤️ 1
    @fortunezhang

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

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

    @zhyu 我是计算函数执行时间,timedelta不适用。
    hahastudio
        6
    hahastudio  
       2015-02-12 23:01:22 +08:00
    1. 性能分析有 timeit 和 profile
    2. python 里 call function 是一个很有代价的操作
    fortunezhang
        7
    fortunezhang  
       2015-02-13 08:50:06 +08:00
    @jamiesun 如果你相求函数或者其他程序运行时间,这个就可以,我一般用这个。否则,可能不行。
    zhyu
        8
    zhyu  
       2015-02-13 09:00:09 +08:00
    @jamiesun 计算函数执行时间用 timeit
    jamiesun
        9
    jamiesun  
    OP
       2015-02-13 14:38:15 +08:00
    @zhyu 但是我是在web运行期间执行,应用有状态
    jamiesun
        10
    jamiesun  
    OP
       2015-02-14 15:47:57 +08:00
    谢谢各位,最后还是回到profile
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1029 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:45 · PVG 03:45 · LAX 12:45 · JFK 15:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.