ishexdigit()

David M. Wilson dw-google.com at botanicus.net
Wed Dec 31 14:51:58 EST 2003


Uwe Schmitt <uwe.schmitt at procoders.net> wrote...

> better/faster/more pythonic:
> 
>          return reduce(lambda a,b: a and ishexdigit(b), strng, True)


How is it better, or faster, or more Pythonic? The only thing I can
see here that is 'more Pythonic' is blatant iterator abuse, which does
nothing for the readability or efficiency of the code. :)

What happened to good old fashioned functions, eh?

    def is_hex(s, (z,n,a,f) = map(ord, '09af')):
        for c in s:
            o = ord(c) | 32 # OR lowercase bit.
            if not (( o >= z and o <= n ) or ( o >= a and o <= f )):
                return False

        return True


This could of course be improved upon too. For:

    profile.run("[ func('CAFEBABEAAAABBBBCCCCDDDDEEEEFFFF') \
        for x in xrange(500000) ]")
    profile.run("[ func('CAFEBABEAAAABBBBCCCCDDDDEEEEFXFF') \
        for x in xrange(100000) ]")
    profile.run("[ func('CAFEXABEAAAABBBBCCCCDDDDEEEEFXFF') \
        for x in xrange(100000) ]")


The iterative version looked like this:

         32500002 function calls in 227.260 CPU seconds
         6300002 function calls in 42.990 CPU seconds
         3800002 function calls in 26.700 CPU seconds


The 'grass roots' version looked like this:

         500002 function calls in 21.090 CPU seconds
         100002 function calls in 3.810 CPU seconds
         100002 function calls in 1.770 CPU seconds


Sometimes I really wonder as to whether Python gets it poorly
performing name from it's implementation or it's 'standard' idioms. :)


> Greetings, Uwe

Happy new year, if you aren't already there. Speaking of which, Taxi!


David.




More information about the Python-list mailing list