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

优化 python 程序?

  •  
  •   billgreen1 · Dec 29, 2015 · 5219 views
    This topic created in 3971 days ago, the information mentioned may be changed or developed.
    一个函数,要使用不同的数据(data1, data2, .....data10000,...),不涉及锁。

    单个函数, func(data1),耗时不大,也就几十到 100 毫秒左右,但是要运行的次数很多。
    这样的程序应该怎样优化?

    我试过 multiprocessing ,但是效果不大,感觉 multiprocess 对那种单个耗时很大的程序帮助比较大。
    21 replies  •  2015-12-29 22:33:26 +08:00
    binux
    binux
    Dec 29, 2015
    1
    用 multiprocessing 之后 CPU 占满了吗?占满了就没得提升了,如果没有,要么你的程序不是 CPU 密集型,继续加大线程 /进程数,要么你用得不对。
    billgreen1
    billgreen1
    OP
    Dec 29, 2015
    2
    @binux 占满了, 55555~~~
    clino
    clino
    Dec 29, 2015 via Android
    3
    cython 之类的?
    helloworld00
    helloworld00
    Dec 29, 2015
    4
    先 profile 看到底哪最耗时,从你这么几句看不到代码很难说出什么方法

    想办法有目的性的去优化代码, 数据结构,代码逻辑等

    最后用 pypy
    ericls
    ericls
    Dec 29, 2015 via iPhone
    5
    CPU 密集用 pypy cython 多线程
    IO 密集 用 异步

    看看 GIL 也许有帮助
    ljdawn
    ljdawn
    Dec 29, 2015
    6
    如果是 cpu 密集型的就没法子了...
    DuckJK
    DuckJK
    Dec 29, 2015
    7
    h4x3rotab
    h4x3rotab
    Dec 29, 2015 via iPhone
    8
    mapreduce
    bookocean
    bookocean
    Dec 29, 2015
    9
    都不清楚你这个函数是干嘛的,怎么给意见呢
    9hills
    9hills
    Dec 29, 2015
    10
    分析性能瓶颈,尝试优化,实在不行就用 C 重写相关部分

    其实暴力的办法就是上多机并行处理
    Feiox
    Feiox
    Dec 29, 2015
    11
    其实。。。一个函数几十上百毫秒,已经算耗时大的了。。。。
    Aalen
    Aalen
    Dec 29, 2015
    13
    hadoop 啦~
    zhicheng
    zhicheng
    Dec 29, 2015 via Android
    14
    Numpy
    ProfFan
    ProfFan
    Dec 29, 2015
    15
    @est 非常感谢🙏收藏了~
    pynix
    pynix
    Dec 29, 2015
    16
    这耗时已经很长了好吧。。。。。
    ryd994
    ryd994
    Dec 29, 2015 via Android
    17
    不要把函数直接挂给 multiprocessing
    fork 的开销也不小的
    正确的是写一个 wrapper ,让一个进程处理几十个
    m8syYID5eaas8hF7
    m8syYID5eaas8hF7
    Dec 29, 2015
    18
    用 C++写一下那个函数试试看(来自 python 初学者的建议)
    ethego
    ethego
    Dec 29, 2015
    19
    为什么要开进程?这明显只要开线程就够了
    glogo
    glogo
    Dec 29, 2015
    20
    multiprocessing + 协成,我是这样的, IO 密集型,性能是 multiprocessing + 线程的 5 倍
    asxalex
    asxalex
    Dec 29, 2015
    21
    尝试优化函数内部算法,数据结构吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3547 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 88ms · UTC 10:07 · PVG 18:07 · LAX 02:07 · JFK 05:07
    ♥ Do have faith in what you're doing.