推荐学习书目
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
simoncos
V2EX  ›  Python

Python 功能点实现:函数级/代码块级计时器

  •  
  •   simoncos ·
    simoncos · Jan 5, 2019 · 3147 views
    This topic created in 2701 days ago, the information mentioned may be changed or developed.
    Supplement 1  ·  Jan 5, 2019
    有没有什么更好的实现方法呢?
    Supplement 2  ·  Jan 5, 2019
    主要涉及装饰器和上下文管理器
    11 replies    2019-01-05 23:50:44 +08:00
    fanhaipeng0403
        1
    fanhaipeng0403  
       Jan 5, 2019
    卧槽 我刚写了个类似的文章~
    simoncos
        2
    simoncos  
    OP
       Jan 5, 2019
    @fanhaipeng0403 哈哈,握手,发出来看看啊
    fanhaipeng0403
        4
    fanhaipeng0403  
       Jan 5, 2019
    @simoncos 可能没啥用 哈哈
    Allianzcortex
        5
    Allianzcortex  
       Jan 5, 2019 via iPhone
    我能想到的基本也就这两种方法了...
    simoncos
        7
    simoncos  
    OP
       Jan 5, 2019
    @Allianzcortex 哈哈哈哈有种被钦定的感觉
    Allianzcortex
        8
    Allianzcortex  
       Jan 5, 2019 via iPhone
    @simoncos 说明 best practice 是全球通用的 !😷
    neoblackcap
        9
    neoblackcap  
       Jan 5, 2019   ❤️ 2
    如果只是简单的计数,我觉得这个实现问题不大,如果是为了做性能剖析采样的话。

    uber 的 pyflame,无入侵式,可以生成火焰图,看火焰图就差不多了。

    如果想跟进一步做 timer 的话,可以使用 CPython 的 Profiling API,Pycharm 也是用对应的 API 实现 Debugger 的。精确到行级,性能更高。
    simoncos
        10
    simoncos  
    OP
       Jan 5, 2019
    @neoblackcap pyflame 之前不了解,无侵入式倒是很有意思,不知道是怎么做到的,谢谢推荐。profile 的话场景感觉有点不一样,目前我工程上主要是用计时器在生产时输出日志。
    neoblackcap
        11
    neoblackcap  
       Jan 5, 2019
    @simoncos 非侵入式,当然是要考虑 dtrace, bpf, ptrace 之类的工具,然后再对应回你的源代码。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1026 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 18:41 · PVG 02:41 · LAX 11:41 · JFK 14:41
    ♥ Do have faith in what you're doing.