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

Python 性能的大杀器

  •  
  •   nooper · 2016-07-16 23:01:16 +08:00 · 4325 次点击
    这是一个创建于 2847 天前的主题,其中的信息可能已经有所发展或是发生改变。

    from package import * especial in the __init__.py.

    这对包会深度递归。 会挨个遍历每一个__init__.py 。 性能会导致很慢。有些情况会导致循环引用以及外部变量不当。 居然有 2000 个行有这些代码。 其中还包含一些比较厉害的包。 声明__all__除外。

    18 条回复    2016-07-18 16:41:21 +08:00
    tigerstudent
        1
    tigerstudent  
       2016-07-16 23:17:43 +08:00
    正常人不会这样做吧
    nooper
        2
    nooper  
    OP
       2016-07-16 23:21:33 +08:00
    --packages
    ---- __init__.py
    ---- a.py

    in a file

    ```python
    import pandas
    import numpy

    ```
    如果我在__init__.py

    from .a impot *
    这样会导致执行 import pandas, 和 import numpy
    如果导入会执行
    0.720189
    如果不导入
    0.001125000000000001
    julyclyde
        3
    julyclyde  
       2016-07-16 23:22:05 +08:00
    会遍历?
    nooper
        4
    nooper  
    OP
       2016-07-16 23:24:37 +08:00
    为啥我不能 append 呢?什么鬼。声明 all 也不例外。
    nooper
        5
    nooper  
    OP
       2016-07-16 23:28:19 +08:00
    import * 是个极大的败笔。导致了一堆副作用。
    9hills
        6
    9hills  
       2016-07-16 23:39:15 +08:00 via iPhone
    不会用怪败笔,呵呵
    nooper
        7
    nooper  
    OP
       2016-07-16 23:40:30 +08:00 via iPad
    @9hills 包太多了。大概是有几十个第三方包是不合规的。
    lhbc
        8
    lhbc  
       2016-07-17 00:05:24 +08:00
    Oracle, MSSQL, MySQL 性能大杀器
    select * from table where a like '%a%' and b like '%b%' and c like '%c%'
    chineselittleboy
        9
    chineselittleboy  
       2016-07-17 00:18:52 +08:00
    @lhbc 天呐 居然有*和 like~
    binux
        10
    binux  
       2016-07-17 01:59:42 +08:00
    from .a impot * 的时候, a 的脚本一定会被执行的,它的 global 变量有什么是确定的。即使 from .a impot b 也一样。
    而且 import 只加载一次,慢又有什么关系呢
    aec4d
        11
    aec4d  
       2016-07-17 07:38:18 +08:00
    import *对性能来说没有任何问题
    你可以看 werkzeug 的__init__.py 文件 搞什么 lazy loading 按需加载模块 官网都说了 并没有什么卵用
    import 一次之后就会有缓存 虽然 import *会导入很多模块 可是这点内存和时间损失都是微不足道的
    假如你用的是 from xxx import xxx 可是当你使用其他该库的模块的时候 因为模块相互调用 最后也和 import *是一样的

    不推荐使用 import * 不是因为性能问题 而是表意不明或者会污染命名空间 不要本末倒置了
    twl007
        12
    twl007  
       2016-07-17 08:19:19 +08:00 via iPhone
    python 自己不是有缓存么…… 有缓存的情况下不算是问题吧
    zhuangzhuang1988
        13
    zhuangzhuang1988  
       2016-07-17 09:59:48 +08:00
    @aec4d 你说的是长时间运行的程序当然没问题, 但是短暂的程序, 像写写代码再调试的话肯定受不了, 或者是 mercurial 这样的. 要是都 import 进来那每次运行一个命令还不玩死.
    所以 mercurial 专门有解决方法 https://selenic.com/hg/file/tip/mercurial/demandimport.py
    guyskk
        14
    guyskk  
       2016-07-17 10:18:13 +08:00
    @zhuangzhuang1988 命令行里用 from xxx import * 很常见吧,这点时间根本感觉不出来
    zhuangzhuang1988
        15
    zhuangzhuang1988  
       2016-07-17 12:06:10 +08:00
    @guyskk 嗯, 没说所有的都必须使用 lazy 技术, 简单的来说 你的 module 里加载太多, 比如里面还有个 time.sleep(1000), 但是实际上你这个模块根本没用到. 咋办...
    jjx
        16
    jjx  
       2016-07-18 12:02:03 +08:00
    除非你是 gui 程序, 注重首次启动的时间, 在服务端, 这个就不是问题
    nooper
        17
    nooper  
    OP
       2016-07-18 13:33:16 +08:00
    @jjx 对啊。就是很坑。
    cszhiyue
        18
    cszhiyue  
       2016-07-18 16:41:21 +08:00
    不要总想搞个大新闻
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   844 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:22 · PVG 03:22 · LAX 12:22 · JFK 15:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.