control structures (was "Re: Sins")

Darrell darrell at dorb.com
Thu Jan 6 19:16:41 EST 2000


Using an exception as a "switch" statement is a cool idea.
A dictionary approach comes to mind, with so many states.

From: "Skip Montanaro" <skip at mojam.com>
>
> Running the above yields:
>
> Time: 5.52181506157
> Time: 4.23444199562
>

Skip you need a faster computer :)
I get these times, with a 400mhz PII.
Time: 2.65600001812
Time: 1.90699994564
Yes I know, who cares.

Increased size by 100x
size=100000
Time: 265.381999969
Time: 191.114000082


# Dictionary based switch statement
def action():
    pass

stateDic={}
s = string.letters + string.digits
for i in s:
    stateDic[i]=action

def defaultCase():
    pass

def func3(size, stateDic, default=defaultCase):
    s = string.letters + string.digits
    for i in xrange(size):
        for l in s:
            action=stateDic.get(l,default)

size=100000
t1=time.time()
func3(size, stateDic)
print 'Time:',time.time()-t1

I didn't call the action since our previous examples just did "pass"
Time: 8.51600003242

The -X switch was interesting. But I doubt it would be useful in general ???
It amazes me what many eyes can see.

--Darrell







More information about the Python-list mailing list