On Monday, November 25, 2013 3:04:13 PM UTC+1, Edward K. Ream wrote:
>
> On Sun, Nov 24, 2013 at 7:53 PM, Terry Brown <[email protected]<javascript:>
> > wrote:
>
> QQQ
> Some other function: The second one is short and faster than the first.
>
> def computeLeadingWhitespaceWidth (s,tab_width):
>
> w = 0
> for ch in s:
> if ch == ' ':
> w += 1
> elif ch == '\t':
> w += (abs(tab_width) - (w % abs(tab_width)))
> else:
> break
> return w
>
>
> def computeLeadingWhitespaceWidth2(s, tab_width):
> t = s.expandtabs(abs(tab_width))
> return len(t) - len(t.lstrip())
>
>
> I don't know - are such micro-optimizations welcome or just a distraction?
> QQQQQ
>
>
> The proposed change, though perhaps amusing, is not simply a distraction.
>> It could be extremely bad, for at least the following reasons.
>>
>
Well, I didn't propose a change, but just asked (with other words), if this
is the place to discuss such topics.
Your answer is clear enough.
>
>>
> 1. Although you claim that the two pieces of code are equivalent, you have
> not proven your claim. A unit test demonstrating that the two pieces of
> code are equivalent **for all relevant strings** would be essential.
>
Yes, you are right. I just used a Python function that expands tabs in
strings and applied to the existing function.
> 2. The existing code, though perhaps verbose, expresses an intention.
> Even if the second piece of code were faster than the first, it doesn't
> express any intention at all.
>
I don't understand what you mean by 'not expressing an intention'. For me,
the name of the method expresses the intention well enough.
> 3. You seem to have no idea whatever that this is fundamentally important
> code. If affects Leo's @file read code as well as Leo's import code.
> Therefore, any change whatever to this code, no matter how "innocent" it
> seems (and the new code is far from an innocent-appearing change) could
> damage existing external files.
>
That's just a (may I say 'unfair') conjecture. The fact that this code is
in the very core shows that it must be important. What this function and
its corresponding whitespace handling functions do, probably affects almost
every aspect of an outline or of Python code. And as such they must be
foolproof and fast.
>
> 4. As Terry says, there is no reason to change this particular piece of
> code. It is unlikely to be a bottleneck, and even if it were, you would
> have to show that the new version was faster than the old.
>
Well, I did some timings. (If you care, I've attached the file. There even
is an enhanced version of the current function, that moves the call to the
abs function out of the loop.) The new code is about 40% faster than the
old one. If there is a bottleneck, I can't tell; you're the expert.
>
> In short, this change is, at best, irrelevant. At worst, it could
> introduce nasty, data-corrupting bugs.
>
I just try to learn something about the inner workings of Leo. And now I
learned that is is not the place to discuss such code details.
Thanks for being so blunt.
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/groups/opt_out.
import time
def timer(func, *args, **kwargs):
repetitions = kwargs.get("repeat", 10)
start = time.clock()
result = None
i = 0
while i < repetitions:
ret = func(*args)
i += 1
elapsed = time.clock() - start
print result, elapsed
return (elapsed, result)
def computeLeadingWhitespaceWidth (s,tab_width):
"""Originial"""
w = 0
for ch in s:
if ch == ' ':
w += 1
elif ch == '\t':
w += (abs(tab_width) - (w % abs(tab_width)))
else:
break
return w
def computeLeadingWhitespaceWidth1(s,tab_width):
"""Original enhanced"""
w = 0
tw = abs(tab_width)
for ch in s:
if ch == ' ':
w += 1
elif ch == '\t':
w += (tw - (w % tw))
else:
break
return w
def computeLeadingWhitespaceWidth2(s, tab_width):
"""ALternative"""
t = s.expandtabs(abs(tab_width))
return len(t) - len(t.lstrip())
def testWidth():
s = "\t \t \t \t Leo\tis\ta\tbrilliant\tprogram."
print s.expandtabs(4)
timer(computeLeadingWhitespaceWidth, s, -4, repeat=1000000)
timer(computeLeadingWhitespaceWidth1, s, -4, repeat=1000000)
timer(computeLeadingWhitespaceWidth2, s, -4, repeat=1000000)
if __name__ == "__main__":
testWidth()