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

Python 在文本比较的时候发生了什么?先比较长度,还是?

  •  
  •   fy ·
    fy0 · Apr 20, 2015 · 3284 views
    This topic created in 4070 days ago, the information mentioned may be changed or developed.
    写一个脚本,因为涉及到很多行的文本,所以要跑几十分钟。

    这个时候看到那个 == 就在想,Python的文本比较是怎么实现的?如果我手动len()先比一下会不会快一点?求老司机带路
    6 replies    2015-04-20 20:51:39 +08:00
    oott123
        1
    oott123  
       Apr 20, 2015   ❤️ 1
    我认为这种自作聪明的“优化”只会更慢……
    毕竟写 Python 的那群人想的不比你少 :)
    lichun
        2
    lichun  
       Apr 20, 2015   ❤️ 2
    lichun@ubuntu:~$ python -m profile str_equal_test.py
    3 function calls in 0.033 seconds

    lichun@ubuntu:~$ python -m profile str_len_equal_test.py
    200003 function calls in 1.206 seconds
    fy
        3
    fy  
    OP
       Apr 20, 2015
    @lichun 多谢,有心了。
    laike9m
        4
    laike9m  
       Apr 20, 2015
    字符串比较难道不是从头开始一个一个字符比么,至少是类似的吧
    lerry
        5
    lerry  
       Apr 20, 2015
    pypy 会不会快一点?纯猜测
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5274 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 07:44 · PVG 15:44 · LAX 00:44 · JFK 03:44
    ♥ Do have faith in what you're doing.