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

你们有没有发现 py3.6 运算速度变快了

  •  
  •   a87150 · Jan 12, 2017 · 6146 views
    This topic created in 3399 days ago, the information mentioned may be changed or developed.
    import time
    import multiprocessing
    
    def totaltime(ct):
        st = time.time()
        a = 100.33  
        b = 23.33  
        for v in range(ct):
            b = 1 + b
            c = a * b
        print("total time:", time.time()-st)
    
    ct = 25000000
    
    if __name__ == '__main__':
        for i in range(multiprocessing.cpu_count()):
            p = multiprocessing.Process(target=totaltime, args=(ct,))
            p.start()
        for p in multiprocessing.active_children():
            print('Child process name: ' + p.name + ' id: ' + str(p.pid))
    

    这段代码以前要 5s ,现在同一台机器只要 3s 了

    import sys, time
    stdout = sys.stdout
    
    BAILOUT = 16
    MAX_ITERATIONS = 1000
    
    class Iterator:
      def __init__(self):
        print('Rendering...')
        for y in range(-39, 39):
          stdout.write('\n')
          for x in range(-39, 39):
            i = self.mandelbrot(x/40.0, y/40.0)
            
            if i == 0:
              stdout.write('*')
            else:
              stdout.write(' ')
        
      def mandelbrot(self, x, y):
        cr = y - 0.5
        ci = x
        zi = 0.0
        zr = 0.0
        i = 0
    
        while True:
          i += 1
          temp = zr * zi
          zr2 = zr * zr
          zi2 = zi * zi
          zr = zr2 - zi2 + cr
          zi = temp + temp + ci
     		  
          if zi2 + zr2 > BAILOUT:
            return i
          if i > MAX_ITERATIONS:
            return 0
    
    t = time.time()
    Iterator()
    print('\nPython Elapsed %.02f' % (time.time() - t))
    

    这段以前要 1.2s ,现在同一台机器只要 0.95s 了

    14 replies    2017-01-12 11:09:02 +08:00
    PythonAnswer
        1
    PythonAnswer  
       Jan 12, 2017
    跟 2.7 比比?
    imn1
        2
    imn1  
       Jan 12, 2017
    @PythonAnswer
    你不能要求 win10 跟 win xp 比的
    mokeyjay
        3
    mokeyjay  
       Jan 12, 2017 via iPhone
    @imn1 你的意思是 2.7 的运算速度更快?→_→
    imn1
        4
    imn1  
       Jan 12, 2017
    @mokeyjay
    你 @错人了
    Ahri
        5
    Ahri  
       Jan 12, 2017
    运行 1000 遍取平均值再说
    daimoon
        6
    daimoon  
       Jan 12, 2017
    python2 -V
    Python 2.7.10
    python2 t1.py
    Child process name: Process-1 id: 697
    Child process name: Process-3 id: 699
    Child process name: Process-4 id: 700
    Child process name: Process-2 id: 698
    ('total time:', 11.26904296875)
    ('total time:', 11.378209829330444)
    ('total time:', 11.391005039215088)
    ('total time:', 11.501654863357544)

    python3 -V
    Python 3.5.1
    python3 t1.py
    Child process name: Process-1 id: 707
    Child process name: Process-4 id: 710
    Child process name: Process-2 id: 708
    Child process name: Process-3 id: 709
    total time: 8.77907681465149
    total time: 8.84126591682434
    total time: 8.84130597114563
    total time: 8.845878839492798
    dracarysX
        7
    dracarysX  
       Jan 12, 2017
    有可能是 range 的问题。 python3 优化了 range ,返回的是一个迭代器。
    daimoon
        8
    daimoon  
       Jan 12, 2017
    python3.6 t1.py
    Child process name: Process-2 id: 1191
    Child process name: Process-1 id: 1190
    Child process name: Process-3 id: 1192
    Child process name: Process-4 id: 1193
    total time: 6.99714207649231
    total time: 7.0236029624938965
    total time: 7.036419868469238
    total time: 7.042246103286743
    daimoon
        9
    daimoon  
       Jan 12, 2017
    测试代码改为: xrange 。

    python2 t1.py
    Child process name: Process-1 id: 1234
    Child process name: Process-3 id: 1236
    Child process name: Process-4 id: 1237
    Child process name: Process-2 id: 1235
    ('total time:', 6.006057024002075)
    ('total time:', 6.011800050735474)
    ('total time:', 6.023754119873047)
    ('total time:', 6.047795057296753)
    daimoon
        10
    daimoon  
       Jan 12, 2017
    python3 任重道远啊。
    Allenqjy
        11
    Allenqjy  
       Jan 12, 2017 via iPhone
    赶快干死 2.7
    gimp
        12
    gimp  
       Jan 12, 2017
    py3 中的 range 就是 py2 中的 xrange
    fy
        13
    fy  
       Jan 12, 2017
    @gimp 不是。 xrange 有长度限制,不能超越机器位长, py3 的 range 没有。
    gimp
        14
    gimp  
       Jan 12, 2017
    @fy Thx ,我再去了解一下
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2534 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 06:52 · PVG 14:52 · LAX 23:52 · JFK 02:52
    ♥ Do have faith in what you're doing.