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

做 OJ 时为什么 Python3 慢过 Python2?

  •  
  •   charadeyouare · 2017-03-07 23:10:04 +08:00 · 3286 次点击
    这是一个创建于 2599 天前的主题,其中的信息可能已经有所发展或是发生改变。

    PAT Basic 1028 https://www.patest.cn/contests/pat-b-practise/1028 思路很简单,但用 py3 老是在最后一个测试点超时,在网上找的 py2 答案却没这个问题。 PAT 提供 3.4.2 和 2.7.9 。抄来的代码如下。把 raw_input 和 print 改了也没用...

    from sys import exit    
    
    def isValid( birth ):
        if birth <= "2014/09/06" and birth >= "1814/09/06":
            return 0
        else:
             return 1    
    
    str = raw_input()
    num = int(str)
    max = []
    min = []
    validNum = 0    
    
    for i in range(num):
        tmp = raw_input().split()
        if isValid( tmp[1] ) == 0:
            validNum += 1                              
            if len(max) == 0 or max[1] > tmp[1]:
                max = tmp    
    
            if len(min) == 0 or min[1] < tmp[1]:
                min = tmp
    if len(max) != 0:
        print validNum, max[0], min[0]
    else:
        print '0'    
    
    exit(0)
    
    10 条回复    2017-03-09 01:06:09 +08:00
    wingyiu
        1
    wingyiu  
       2017-03-08 00:08:30 +08:00
    py3 就是比 py2 慢,至少 3.6 之前是
    NoAnyLove
        2
    NoAnyLove  
       2017-03-08 04:20:29 +08:00   ❤️ 1
    我一直以为 Python 3 比 Python 2 快来着。。。。。。。 Orz

    具体还是应该看代码吧,比如 Python 3 中大量使用了生成器,`xrange`取消了,`range`的效果等同于 Python 2 中的`xrange`,但是 Python 3 的`range`要比 Python 2 的`xrange`慢不少,看这里:

    ```
    $ python3 -m timeit -s"r = range(33550336)" "for i in r: pass"
    10 loops, best of 3: 835 msec per loop

    $ python2 -m timeit -s"r = xrange(33550336)" "for i in r: pass"
    10 loops, best of 3: 464 msec per loop
    ```

    代码来自: https://mail.python.org/pipermail/python-list/2011-August/609571.html

    还有很多 filter 、 map 之类的函数都变成了返回生成器一样的对象。
    但是。。。。。为啥 Python 3 比 Python 2 慢啊?这不科学啊。。。。
    NoAnyLove
        3
    NoAnyLove  
       2017-03-08 04:21:52 +08:00
    @wingyiu 为啥? Python 3.6 有什么大的改进吗?
    uxstone
        4
    uxstone  
       2017-03-08 09:46:51 +08:00
    都 python 了,没必要比这点快慢了吧......
    开发快才是真的快
    charadeyouare
        5
    charadeyouare  
    OP
       2017-03-08 10:10:22 +08:00 via iPhone
    @uxstone 刷题也快,能用 py 过就肯定用 py 。
    XYxe
        6
    XYxe  
       2017-03-08 16:28:24 +08:00
    应该和 dict 的效率有很大的关系吧
    charadeyouare
        7
    charadeyouare  
    OP
       2017-03-08 18:20:47 +08:00
    @XYxe 没用到 dict 啊。但 dict 基于 hashmap , get 的效率不低吧?
    XYxe
        8
    XYxe  
       2017-03-08 19:11:42 +08:00
    @charadeyouare Python 内部用到 dict 的地方很多啊,比如说在交互模式下,`a=1`一共会创建并销毁 15 个字典对象。我这里测试 2.7 和 3.5 dict 的效率差很多啊。
    charadeyouare
        9
    charadeyouare  
    OP
       2017-03-08 19:38:07 +08:00
    @XYxe 这就尴尬了, 3.6 会比 2.7 快吗?
    NoAnyLove
        10
    NoAnyLove  
       2017-03-09 01:06:09 +08:00
    @uxstone 刷题有影响。有些时候一个相同的算法,用 C/C++/Java 都能过, Python 却会 TLE , Orz
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2831 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:36 · PVG 22:36 · LAX 07:36 · JFK 10:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.