Sion Arrowsmith wrote:

> Daniel Dittmar  <[EMAIL PROTECTED]> wrote:
>>         if line [:1] == '#':
> 
> What's wrong with line[0] == '#' ? (For one thing, it's fractionally
> faster than [:1].)
> 
line[0] assumes that the line isn't blank. If the input iterator is a file 
then that will hold true, but if you were ever to reuse CommentStripper on 
a list of strings which didn't have a trailing newline it would break at 
the first blank string.

Personally I would use:

  if line.startswith('#'):

which takes about three times as long to execute but I think reads more 
clearly.

timeit.py -s "line=' hello world'" "line[:1]=='#'"
1000000 loops, best of 3: 0.236 usec per loop

timeit.py -s "line=' hello world'" "line[0]=='#'"
1000000 loops, best of 3: 0.218 usec per loop

timeit.py -s "line=' hello world'" "line.startswith('#')"
1000000 loops, best of 3: 0.639 usec per loop
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to