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

Python 字符串替换效率 replace> re.sub

  •  
  •   sunhk25 · 2019-05-08 10:38:54 +08:00 · 2884 次点击
    这是一个创建于 1807 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 单纯的替换,正则的效率比 replace 低三倍左右
    • 那什么时候正则的效率比 replace 高呢?
    import re,timeit
    str = "abcdefghigkqpwueriwutghlskajdf,mnbz,xcnvoiequynf12341234321065498+7mnbz,xcnvoiequynf12341234321065498+7mnbz,xcnvoiequynf12341234321065498+7mnbz,xcnvoiequynf12341234321065498+7"
    
    # pattern1
    start = timeit.default_timer()
    for i in range(1000000):
        tst = re.sub("mn|123|[abcdef7]", "", str)
    stop = timeit.default_timer()
    print('pattern1', 'Time: ', stop - start)
    
    # pattern2
    start = timeit.default_timer()
    for i in range(1000000):
        tst = str.replace("mn", "")
        tst = tst.replace("123", "")
        tst = tst.replace("a", "")
        tst = tst.replace("b", "")
        tst = tst.replace("c", "")
        tst = tst.replace("d", "")
        tst = tst.replace("e", "")
        tst = tst.replace("f", "")
        tst = tst.replace("7", "")
    
    stop = timeit.default_timer()
    print('pattern2', 'Time: ', stop - start)
    
    # pattern3
    start = timeit.default_timer()
    for i in range(1000000):
        tst = re.sub("mn", "", str)
    stop = timeit.default_timer()
    print('pattern3', 'Time: ', stop - start)
    
    # pattern4
    start = timeit.default_timer()
    for i in range(1000000):
        tst = str.replace("mn", "")
    
    stop = timeit.default_timer()
    print('pattern4', 'Time: ', stop - start)
    # 输出
    # pattern1 Time:  10.851562640495892
    # pattern2 Time:  4.726493571613192
    # pattern3 Time:  1.5144934460814898
    # pattern4 Time:  0.5535754144044809
    
    6 条回复    2019-05-08 13:45:24 +08:00
    zagfai
        1
    zagfai  
       2019-05-08 11:02:16 +08:00
    原理上就是差很远,sub 的适用性广很多。
    Hieast
        2
    Hieast  
       2019-05-08 11:05:38 +08:00
    你用 ipython 自己跑一下下面的代码吧,你的写法有两个很大的问题:
    1. 用正则确不先 compile
    2. 居然用 str 做变量名

    https://gist.github.com/hieast/1fc9e5dad298a62178236c0c7bf9f035.js
    sunhk25
        3
    sunhk25  
    OP
       2019-05-08 11:47:54 +08:00
    @Hieast 我测试的时候也做了用 compile 和不用的区别,感觉没有太大提升,这个例子就没有放里。变量名 abc 啥的我都是随手写的啦,没有计较。
    Hieast
        4
    Hieast  
       2019-05-08 12:59:56 +08:00
    @sunhk25 我测的情况是 compile 后 py27 和 py36 都减少了 40% 的运行时间, 正则速度和 replace 差距都不超过一倍,感觉效率方面基本没有讨论的意义了。
    一楼的朋友说的很好,正则灵活多了。
    whoami9894
        5
    whoami9894  
       2019-05-08 13:04:23 +08:00
    @sunhk25
    用 abc 做变量名和用 str 做变量名可不是一回事
    ipwx
        6
    ipwx  
       2019-05-08 13:45:24 +08:00
    @Hieast re.sub 有编译缓存。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3471 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:07 · PVG 08:07 · LAX 17:07 · JFK 20:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.