哪种方法可以更快速地计算没有空格的字符数? 哪个更pythonic?
def sent_length(sentence):
return sum(1 for c in sentence if c != ' ')
或
def sent_length(sentence):
return len(sentence.replace(" ", ""))
或
import re
pattern = re.compile(r'\s+')
def sent_length(sentence):
return len(re.sub(pattern, '', sentence))
答案 0 :(得分:3)
您可以从python -m timeit
获取时间:
[matt tmp] python -m timeit "sum(1 for c in 'blah blah blah' if c != ' ')"
100000 loops, best of 3: 2.96 usec per loop
[matt benchmark] python -m timeit -s "import re; pattern = re.compile(r'\s+')" "len(pattern.sub('', 'blah blah blah'))"
100000 loops, best of 3: 2.2 usec per loop
[matt tmp] python -m timeit "len(''.join('blah blah blah'.split()))"
1000000 loops, best of 3: 0.785 usec per loop
[matt tmp] python -m timeit "len('blah blah blah'.replace(' ', ''))"
1000000 loops, best of 3: 0.437 usec per loop
[matt tmp] python -m timeit "len('blah blah blah') - 'blah blah blah'.count(' ')"
1000000 loops, best of 3: 0.384 usec per loop
这将帮助您确定最快的是什么。更pythonic?我的表现最快,因为总是非常重要。
答案 1 :(得分:0)
就更快而言,你可以自己测试一下。
from datetime import datetime
start = datetime.now()
# some method
end = datetime.now()
diff = end-start
就更多的pythonic来说,我不相信他们中的任何一个更pythonic
。它们都是大多数人都会接受的有效实现。这只是一种略有不同的方法。通常,正则表达式运行时间会稍长。