import time
def timeo(fun, n=1000):
def void( ): pass
start = time.clock( )
for i in range(n): void( )
stend = time.clock( )
overhead = stend - start
start = time.clock( )
for i in range(n): fun( )
stend = time.clock( )
fulltime = stend-start
return fun.__name__, fulltime-overhead
for f in solution1, solution2, solution3:
print "%s: %.2f" % timeo(f)
>>> paragraph = "five hundred to one hundred words separated by spaces"
>>> array_of_strings = set(['hundred', 'spaces', ]) # make a set..
>>> print set(paragraph.split()).intersection(array_of_strings)
set(['hundred', 'spaces'])
如果使用list,请先对其排序并使用二进制搜索。在
但使用字典可能会更好;)
我会把你的5500个字符串数组转换成一个集合,只使用集合交集。在
下面是你计时的方法。在
阅读timeit模块。这是另一个教程:http://diveintopython.net/performance_tuning/timeit.html
^{pr2}$相关问题 更多 >
编程相关推荐