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

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

  •  
  •   fy ·
    fy0 · 2015-04-20 18:59:48 +08:00 · 2753 次点击
    这是一个创建于 3306 天前的主题,其中的信息可能已经有所发展或是发生改变。
    写一个脚本,因为涉及到很多行的文本,所以要跑几十分钟。

    这个时候看到那个 == 就在想,Python的文本比较是怎么实现的?如果我手动len()先比一下会不会快一点?求老司机带路
    6 条回复    2015-04-20 20:51:39 +08:00
    oott123
        1
    oott123  
       2015-04-20 19:26:40 +08:00   ❤️ 1
    我认为这种自作聪明的“优化”只会更慢……
    毕竟写 Python 的那群人想的不比你少 :)
    lichun
        2
    lichun  
       2015-04-20 19:44:51 +08:00   ❤️ 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
       2015-04-20 20:02:19 +08:00
    @lichun 多谢,有心了。
    laike9m
        4
    laike9m  
       2015-04-20 20:42:21 +08:00
    字符串比较难道不是从头开始一个一个字符比么,至少是类似的吧
    lerry
        5
    lerry  
       2015-04-20 20:46:18 +08:00
    pypy 会不会快一点?纯猜测
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1345 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:17 · PVG 07:17 · LAX 16:17 · JFK 19:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.