V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 4589 views
    This topic created in 3776 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
        1
    binux  
       Dec 29, 2015
    用 multiprocessing 之后 CPU 占满了吗?占满了就没得提升了,如果没有,要么你的程序不是 CPU 密集型,继续加大线程 /进程数,要么你用得不对。
    billgreen1
        2
    billgreen1  
    OP
       Dec 29, 2015
    @binux 占满了, 55555~~~
    clino
        3
    clino  
       Dec 29, 2015 via Android
    cython 之类的?
    helloworld00
        4
    helloworld00  
       Dec 29, 2015
    先 profile 看到底哪最耗时,从你这么几句看不到代码很难说出什么方法

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

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

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

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