Engineer as a Lifestyle @tenkoma

What We Find Changes Who We Become -- Peter Morville著『アンビエント・ファインダビリティ 』

皆Py書房 -- That's a side of The Python I didn't know(2)

p.74 表1 複数の不等号を組み合わせた比較

>>> def is20s(int_):
	if 20 <= int_ < 30:
		return True
	return False

>>> is20s(19)
False
>>> is20s(20)
True
>>> is20s(29)
True
>>> is20s(30)
False

p.77,78 findメソッドと比較演算子in

>>> s = "this is ASCII string"
>>> s.find("ASCII")
8
>>> s.find("hoge")
-1
>>> "ASCII" in s
True

どうみても2.3以後ならinを使った方がコードが読みやすくなっていい気がする.実行速度は?(intel iMac, core 2 duo 2.16GHz)

>>> import time
>>> def findtime(count):
	s = "this is ASCII string"
	start = time.time()
	for i in range(count):
		s.find("ASCII",3,-4)
	end = time.time()
	print end - start
>>> def slicetime(count):
	s = "this is ASCII string"
	start = time.time()
	for i in range(count):
		"ASCII" in s[3:-4]
	end = time.time()
	print end - start
>>> findtime(10000000)
5.4719979763
>>> slicetime(10000000)
4.76505899429

あんまり大差ない.inが若干早いかも
Thinkpad X31, Pentium M 1.3GHz

>>> findtime(10000000)
12.5379998684
>>> slicetime(10000000)
7.74199986458

はっきりと差が付きました.