Re: Fastest way to calculate leading whitespace

2010-05-11 Thread dasacc22
On May 10, 2:25 am, Stefan Behnel wrote: > Stefan Behnel, 10.05.2010 08:54: > > > > > > > dasacc22, 08.05.2010 19:19: > >> This is a simple question. I'm looking for the fastest way to > >> calculate the leading whitespace (as a string, ie ' '). > > > Here is an (untested) Cython 0.13 solution: >

Re: Fastest way to calculate leading whitespace

2010-05-10 Thread Stefan Behnel
Stefan Behnel, 10.05.2010 08:54: dasacc22, 08.05.2010 19:19: This is a simple question. I'm looking for the fastest way to calculate the leading whitespace (as a string, ie ' '). Here is an (untested) Cython 0.13 solution: from cpython.unicode cimport Py_UNICODE_ISSPACE def leading_w

Re: Fastest way to calculate leading whitespace

2010-05-09 Thread Stefan Behnel
dasacc22, 08.05.2010 19:19: This is a simple question. I'm looking for the fastest way to calculate the leading whitespace (as a string, ie ''). Here is an (untested) Cython 0.13 solution: from cpython.unicode cimport Py_UNICODE_ISSPACE def leading_whitespace(unicode ustring):

Re: Fastest way to calculate leading whitespace

2010-05-09 Thread dasacc22
On May 9, 8:28 am, John Machin wrote: > dasacc22 gmail.com> writes: > > > > > U presume entirely to much. I have a preprocessor that normalizes > > documents while performing other more complex operations.  Theres > > nothing buggy about what im doing > > Are you sure? > > Your "solution" calcula

Re: Fastest way to calculate leading whitespace

2010-05-09 Thread Mark Dickinson
On May 9, 6:13 am, Steven D'Aprano wrote: > On Sat, 08 May 2010 13:46:59 -0700, Mark Dickinson wrote: > >> However, s[:-len(t)] should be both faster and correct. > > > Unless len(t) == 0, surely? > > Doh! The hazards of insufficient testing. Thanks for catching that. I have a love-hate relations

Re: Fastest way to calculate leading whitespace

2010-05-09 Thread John Machin
dasacc22 gmail.com> writes: > > U presume entirely to much. I have a preprocessor that normalizes > documents while performing other more complex operations. Theres > nothing buggy about what im doing Are you sure? Your "solution" calculates (the number of leading whitespace characters) + (th

Re: Fastest way to calculate leading whitespace

2010-05-09 Thread Wolfram Hinderer
On 8 Mai, 21:46, Steven D'Aprano wrote: > On Sat, 08 May 2010 12:15:22 -0700, Wolfram Hinderer wrote: > > Returning s[:-1 - len(t)] is faster. > > I'm sure it is. Unfortunately, it's also incorrect. > However, s[:-len(t)] should be both faster and correct. Ouch. Thanks for correcting me. No, I'

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Steven D'Aprano
On Sat, 08 May 2010 14:27:32 -0700, dasacc22 wrote: > U presume entirely to much. I have a preprocessor that normalizes > documents while performing other more complex operations. Theres > nothing buggy about what im doing I didn't *presume* anything, I took your example code and ran it and dis

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Steven D'Aprano
On Sat, 08 May 2010 13:46:59 -0700, Mark Dickinson wrote: >> However, s[:-len(t)] should be both faster and correct. > > Unless len(t) == 0, surely? Doh! The hazards of insufficient testing. Thanks for catching that. -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread dasacc22
On May 8, 2:46 pm, Steven D'Aprano wrote: > On Sat, 08 May 2010 12:15:22 -0700, Wolfram Hinderer wrote: > > On 8 Mai, 20:46, Steven D'Aprano > > wrote: > > >> def get_leading_whitespace(s): > >>     t = s.lstrip() > >>     return s[:len(s)-len(t)] > > >> >>> c = get_leading_whitespace(a) > >> >>>

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread dasacc22
On May 8, 5:18 pm, Patrick Maupin wrote: > On May 8, 1:16 pm, dasacc22 wrote: > > > > > > > On May 8, 12:59 pm, Patrick Maupin wrote: > > > > On May 8, 12:19 pm, dasacc22 wrote: > > > > > Hi > > > > > This is a simple question. I'm looking for the fastest way to > > > > calculate the leading wh

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Patrick Maupin
On May 8, 1:16 pm, dasacc22 wrote: > On May 8, 12:59 pm, Patrick Maupin wrote: > > > > > On May 8, 12:19 pm, dasacc22 wrote: > > > > Hi > > > > This is a simple question. I'm looking for the fastest way to > > > calculate the leading whitespace (as a string, ie '    '). > > > > Here are some dif

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread dasacc22
U presume entirely to much. I have a preprocessor that normalizes documents while performing other more complex operations. Theres nothing buggy about what im doing On May 8, 1:46 pm, Steven D'Aprano wrote: > On Sat, 08 May 2010 10:19:16 -0700, dasacc22 wrote: > > Hi > > > This is a simple quest

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Mark Dickinson
On May 8, 8:46 pm, Steven D'Aprano wrote: > On Sat, 08 May 2010 12:15:22 -0700, Wolfram Hinderer wrote: > > On 8 Mai, 20:46, Steven D'Aprano > > wrote: > > >> def get_leading_whitespace(s): > >>     t = s.lstrip() > >>     return s[:len(s)-len(t)] > > >> >>> c = get_leading_whitespace(a) > >> >>>

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Steven D'Aprano
On Sat, 08 May 2010 12:15:22 -0700, Wolfram Hinderer wrote: > On 8 Mai, 20:46, Steven D'Aprano > wrote: > >> def get_leading_whitespace(s): >>     t = s.lstrip() >>     return s[:len(s)-len(t)] >> >> >>> c = get_leading_whitespace(a) >> >>> assert c == leading_whitespace >> >> Unless your string

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Wolfram Hinderer
On 8 Mai, 20:46, Steven D'Aprano wrote: > def get_leading_whitespace(s): >     t = s.lstrip() >     return s[:len(s)-len(t)] > > >>> c = get_leading_whitespace(a) > >>> assert c == leading_whitespace > > Unless your strings are very large, this is likely to be faster than any > other pure-Python

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Steven D'Aprano
On Sat, 08 May 2010 10:19:16 -0700, dasacc22 wrote: > Hi > > This is a simple question. I'm looking for the fastest way to calculate > the leading whitespace (as a string, ie ''). Is calculating the amount of leading whitespace really the bottleneck in your application? If not, then trying

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread dasacc22
On May 8, 12:59 pm, Patrick Maupin wrote: > On May 8, 12:19 pm, dasacc22 wrote: > > > > > > > Hi > > > This is a simple question. I'm looking for the fastest way to > > calculate the leading whitespace (as a string, ie '    '). > > > Here are some different methods I have tried so far > > --- sol

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Patrick Maupin
On May 8, 12:19 pm, dasacc22 wrote: > Hi > > This is a simple question. I'm looking for the fastest way to > calculate the leading whitespace (as a string, ie '    '). > > Here are some different methods I have tried so far > --- solution 1 > > a = '    some content\n' > b = a.strip() > c = ' '*(l

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Daniel Skinner
sorry, my mistake it runs faster (looking at the wrong line of code). But the first two solutions are still faster. On Sat, May 8, 2010 at 12:48 PM, Daniel Skinner wrote: > That solution actually runs slower then the generator method. > > > On Sat, May 8, 2010 at 12:33 PM, Shashank Singh < > sha

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Daniel Skinner
That solution actually runs slower then the generator method. On Sat, May 8, 2010 at 12:33 PM, Shashank Singh < shashank.sunny.si...@gmail.com> wrote: > > > On Sat, May 8, 2010 at 10:49 PM, dasacc22 wrote: > >> Hi >> >> This is a simple question. I'm looking for the fastest way to >> calculate t

Re: Fastest way to calculate leading whitespace

2010-05-08 Thread Shashank Singh
On Sat, May 8, 2010 at 10:49 PM, dasacc22 wrote: > Hi > > This is a simple question. I'm looking for the fastest way to > calculate the leading whitespace (as a string, ie ''). > > Here are some different methods I have tried so far > --- solution 1 > > a = 'some content\n' > b = a.strip(

Fastest way to calculate leading whitespace

2010-05-08 Thread dasacc22
Hi This is a simple question. I'm looking for the fastest way to calculate the leading whitespace (as a string, ie ''). Here are some different methods I have tried so far --- solution 1 a = 'some content\n' b = a.strip() c = ' '*(len(a)-len(b)) --- solution 2 a = 'some content\n'