更快'如果字符串中的字符'测试 [英] Faster 'if char in string' test
问题描述
我一直在寻找一种方法来加速检测我的
TCP字符串中的无效字符,并想到翻译功能的另一个用途!
如果你是要''翻译''坏字符,然后比较字符串
长度,你会知道该行是否包含
无效字符。新方法比
标准''快10倍以上''如果字符串中的字符''测试!所以 - 这里是代码加样品
时间:
''''''
翻译速度测试
此代码在字符串中查找无效字符,
并在找到时引发异常。
我是测试2种方法:'if string in string''方法,
和一个基于使用''translate''函数和
比较字符串长度。
哇,有什么区别!翻译速度提高了10倍以上!
功能循环秒数/秒
****************** *****************************
在10000 0.171 58479
翻译10000 0.016 624998
'''''
导入mytime
导入字符串
_allchars =无
_deletechars =无
_validchars = string.ascii_letters + string.digits + \
"!@# $%^& *()`〜-_ = + [{]} \\ |;:\''\",<。> /?\t"
def init():
全球_allchars,_deletechars
l = []
a = []
for i in range(256):
a.append(chr(i))
如果不是chr(i)in _validchars:
l.append(chr(i))
_deletechars =''''。join(l)
_allchars =''''。join(a)
def test():
max = 10000
tmr = mytime.Timer()
r =范围(最大)
s ="这是一个用于测试无效字符的字符串。
print tmr.heading
tmr.startit()
for i in r:
for c in s:
if c在_deletechars中:
引发异常(找到无效字符!)
tmr.stopit(max)
print tmr.results( ''在'')
tmr.startit()
我在r中的
:
s2 = s.translate( _allchars,_deletechars)
if len(s2)!= len(s):
引发异常(找到无效字符!)
tmr.stopit(max)
print tmr.results(''Translate'')
init()
if __name__ ==" __ main __":
test()
I was looking for a way to speed up detecting invalid characters in my
TCP string, and thought of yet another use for the translate function!
If you were to ''translate out'' the bad characters, and compare string
lengths afterwards, you would know whether or not the line contained
invalid characters. The new method is more than 10x faster than the
standard ''if char in string'' test! So - here''s the code plus sample
timings:
''''''
Translate Speed Test
This code looks for invalid characters in a string,
and raises an exception when it finds one.
I''m testing 2 methods: the ''if char in string'' method,
and one based on using the ''translate'' function and
comparing string lengths afterwards.
Wow, what a difference! Translate is over 10x faster!
Function Loops Seconds Loops/sec
***********************************************
In 10000 0.171 58479
Translate 10000 0.016 624998
''''''
import mytime
import string
_allchars = None
_deletechars = None
_validchars = string.ascii_letters + string.digits + \
"!@#$%^&*()`~-_=+[{]}\\|;:\''\",<.>/?\t "
def init():
global _allchars, _deletechars
l = []
a = []
for i in range(256):
a.append(chr(i))
if not chr(i) in _validchars:
l.append(chr(i))
_deletechars = ''''.join(l)
_allchars = ''''.join(a)
def test():
max = 10000
tmr = mytime.Timer()
r = range(max)
s = "This is a string to test for invalid characters."
print tmr.heading
tmr.startit()
for i in r:
for c in s:
if c in _deletechars:
raise Exception("Invalid character found!")
tmr.stopit(max)
print tmr.results(''In'')
tmr.startit()
for i in r:
s2 = s.translate(_allchars, _deletechars)
if len(s2) != len(s):
raise Exception("Invalid character found!")
tmr.stopit(max)
print tmr.results(''Translate'')
init()
if __name__ == "__main__":
test()
推荐答案
%^& *()`〜 -_ = + [{]} \\ |;:\''\",<。> /?\ t"
def init():
全球_allchars,_deletechars
l = []
a = []
for i in range(256):
a.append(chr(i))
如果不是chr(i)in _validchars:
l.append(chr(i))
_deletechars =''''。join(l)
_allchars =''''。加入(a)
def test():
max = 10000
tmr = mytime.Timer()
r = range(max)
s ="这是一个测试无效字符的字符串。
print tmr.heading
tmr.startit()
for i in r:
for c in s:
if c in _deletechars:
提高异常(找到无效字符!)
tmr.stopit(max)
print tmr.results(''In'')
tmr.startit()
我在r中的
:
s2 = s.translate(_allchars,_deletechars)
if len(s2)!= len(s):
引发异常(找到无效字符!)
tmr.stopit( max)
print tmr.results(''Translate'')
init()
if __name__ ==" __ main __":
test()
%^&*()`~-_=+[{]}\\|;:\''\",<.>/?\t "
def init():
global _allchars, _deletechars
l = []
a = []
for i in range(256):
a.append(chr(i))
if not chr(i) in _validchars:
l.append(chr(i))
_deletechars = ''''.join(l)
_allchars = ''''.join(a)
def test():
max = 10000
tmr = mytime.Timer()
r = range(max)
s = "This is a string to test for invalid characters."
print tmr.heading
tmr.startit()
for i in r:
for c in s:
if c in _deletechars:
raise Exception("Invalid character found!")
tmr.stopit(max)
print tmr.results(''In'')
tmr.startit()
for i in r:
s2 = s.translate(_allchars, _deletechars)
if len(s2) != len(s):
raise Exception("Invalid character found!")
tmr.stopit(max)
print tmr.results(''Translate'')
init()
if __name__ == "__main__":
test()
Kamilche写道:
Kamilche wrote:
是寻找一种方法来加速检测我的
TCP字符串中的无效字符,并想到翻译功能的另一个用途!
如果你要'翻译''坏字符,并比较字符串
之后的长度,你会知道该行是否包含
无效字符。如果字符串中的字符''测试,新方法比
标准''快10倍以上!所以 - 这里是代码加样品
时间:
was looking for a way to speed up detecting invalid characters in my
TCP string, and thought of yet another use for the translate function!
If you were to ''translate out'' the bad characters, and compare string
lengths afterwards, you would know whether or not the line contained
invalid characters. The new method is more than 10x faster than the
standard ''if char in string'' test! So - here''s the code plus sample
timings:
为了公平比较你应该使用字典而不是字符串
收容测试。
彼得
For a fair comparison you should use a dictionary instead of a string for
the containment test.
Peter
Kamilche写道:
Kamilche wrote:
我正在寻找一种方法来加速检测我的
TCP字符串中的无效字符,并想到翻译功能的另一个用途!
如果你要''翻译''坏字符,然后比较字符串
长度,你会知道该行是否包含
无效字符。如果字符串中的字符''测试,新方法比
标准''快10倍以上!所以 - 这里是代码加样本
时间:
I was looking for a way to speed up detecting invalid characters in my
TCP string, and thought of yet another use for the translate function!
If you were to ''translate out'' the bad characters, and compare string
lengths afterwards, you would know whether or not the line contained
invalid characters. The new method is more than 10x faster than the
standard ''if char in string'' test! So - here''s the code plus sample
timings:
也许将这些片段发布在Python食谱上是个好主意。 />
Reinhold
-
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbr?chte,w?re das bedauerlich。 bei Windows der Umfang eines
" kompletten Betriebssystems" ist,nennt man bei Linux eine Rescuedisk。
- David Kastrup在de.comp.os.unix.linux.misc
Perhaps it would be a good idea to post the snippets at the Python cookbook.
Reinhold
--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbr?chte, w?re das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
这篇关于更快'如果字符串中的字符'测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!