Slicing vs .startswith
Bob Gailer
bgailer at alum.rpi.edu
Mon Sep 22 11:57:41 EDT 2003
At 09:17 AM 9/22/2003, Shu-Hsien Sheu wrote:
>Hi,
>
>I have a question about the comparison of efficiency of string slicing and
>using string.startswith.
>For example, which one of the following would be more efficient, or ,
>moreover, more pythonic?
>
>if aa[:3] == 'abc':
>
>vs
>
>if aa.startswith('abc'):
Here's one way to address this kind of question:
>>> import time
>>> def f():
... st = time.time()
... for i in range(500000):
... if aa.startswith('abc'):pass
... print time.time() - st
...
>>> f()
1.01200008392
>>> def f():
... st = time.time()
... for i in range(500000):
... if aa[:3] == 'abc':pass
... print time.time() - st
...
>>> f()
1.01100003719
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003
More information about the Python-list
mailing list