>
> 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.
>>
>  

> This one bugged me. The attached file tests ALL possible combinationes of 
> blanks and tabs to an arbitrary width of leading whitespace (i.e. a width 
> of 16 results in more 28 million tests). One function times the current 
> function, one the new function, and one compares the results of applying 
> both the functions to the same strings. I guess that takes care of **for 
> all relevant strings**; just use a larger width.
>

-- Reinhard
 

-- 
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:
        result = func(*args)
        i += 1
    elapsed = time.clock() - start
    print "Time elapsed:", 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():
    """
    Time the functions with different functions.
    An arbitrary test string is used.
    Each test is repeated one million times.
    """

    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)

def testAllWidth(n):
    """
    Test all possible combinations of ' ' and '\t' up to <n> charcaters.
    <n> specifies the width of the leading whitespace to be tested.

    Be careful: The number of tests grows exponentially.
    """

    def createTestStrings(n):
        testStrings = [" ", "\t"]
        for i in range(1, n):
            t1 = [s + " " for s in testStrings]
            t2 = [s + "\t" for s in testStrings]
            testStrings = testStrings + t1
            testStrings = testStrings + t2
        # Add some characters at the end of the string
        testStrings = [s + "abc" for s in testStrings]
        print "    Tests to be performed:", len(testStrings)
        return testStrings

    def timeOld(testStrings):
        for s in testStrings:
            w = computeLeadingWhitespaceWidth(s, -4)

    def timeNew(testStrings):
        for s in testStrings:
            w = computeLeadingWhitespaceWidth2(s, -4)

    def compareResults(testStrings):
        errors = 0
        for s in testStrings:
            w1 = computeLeadingWhitespaceWidth(s, -4)
            w2 = computeLeadingWhitespaceWidth2(s, -4)
            if w1 != w2:
                errors += 1
                print "    *** error:", s, w1, w2
        print "    Number of differences:", errors

    print "start"
    print "Create testStrings..."
    testStrings = createTestStrings(n)
    print "Start timing..."
    print "    Old function...",
    timer(timeOld, testStrings, repeat=1)
    print "    New function...",
    timer(timeNew, testStrings, repeat=1)
    print "Compare functions..."
    compareResults(testStrings)
    print "done"

if __name__ == "__main__":
    #testWidth()
    testAllWidth(16)






Reply via email to