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
simoncos
V2EX  ›  Python

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

  •  
  •   simoncos ·
    simoncos · 2019-01-05 19:22:53 +08:00 · 2422 次点击
    这是一个创建于 1909 天前的主题,其中的信息可能已经有所发展或是发生改变。
    第 1 条附言  ·  2019-01-05 19:54:58 +08:00
    有没有什么更好的实现方法呢?
    第 2 条附言  ·  2019-01-05 21:12:48 +08:00
    主要涉及装饰器和上下文管理器
    11 条回复    2019-01-05 23:50:44 +08:00
    fanhaipeng0403
        1
    fanhaipeng0403  
       2019-01-05 21:06:04 +08:00
    卧槽 我刚写了个类似的文章~
    simoncos
        2
    simoncos  
    OP
       2019-01-05 21:07:28 +08:00
    @fanhaipeng0403 哈哈,握手,发出来看看啊
    fanhaipeng0403
        3
    fanhaipeng0403  
       2019-01-05 21:07:58 +08:00
    fanhaipeng0403
        4
    fanhaipeng0403  
       2019-01-05 21:08:12 +08:00
    @simoncos 可能没啥用 哈哈
    Allianzcortex
        5
    Allianzcortex  
       2019-01-05 21:16:00 +08:00 via iPhone
    我能想到的基本也就这两种方法了...
    simoncos
        7
    simoncos  
    OP
       2019-01-05 21:25:02 +08:00
    @Allianzcortex 哈哈哈哈有种被钦定的感觉
    Allianzcortex
        8
    Allianzcortex  
       2019-01-05 21:28:11 +08:00 via iPhone
    @simoncos 说明 best practice 是全球通用的 !😷
    neoblackcap
        9
    neoblackcap  
       2019-01-05 23:02:26 +08:00   ❤️ 2
    如果只是简单的计数,我觉得这个实现问题不大,如果是为了做性能剖析采样的话。

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

    如果想跟进一步做 timer 的话,可以使用 CPython 的 Profiling API,Pycharm 也是用对应的 API 实现 Debugger 的。精确到行级,性能更高。
    simoncos
        10
    simoncos  
    OP
       2019-01-05 23:25:31 +08:00
    @neoblackcap pyflame 之前不了解,无侵入式倒是很有意思,不知道是怎么做到的,谢谢推荐。profile 的话场景感觉有点不一样,目前我工程上主要是用计时器在生产时输出日志。
    neoblackcap
        11
    neoblackcap  
       2019-01-05 23:50:44 +08:00
    @simoncos 非侵入式,当然是要考虑 dtrace, bpf, ptrace 之类的工具,然后再对应回你的源代码。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3263 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:07 · PVG 22:07 · LAX 07:07 · JFK 10:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.