Case insensitive exists()?

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Jan 23 10:43:41 EST 2014


On Wed, Jan 22, 2014 at 09:24:54PM -0700, Larry Martell wrote:
> 
> I am writing something that is part of a django app, that based on
> some web entry from the user, I run a query, get back a list of files
> and have to go receive them and serve them up back to the browser. My
> script is all done and seem to be working, then today I was informed
> it was not serving up all the images. Debugging revealed that it was
> this case issue - I was matching with exists(). As I've said, coding a
> solution is easy, but I fear it will be too slow. Speed is important
> in web apps - users have high expectations. Guess I'll just have to
> try it and see.

How long does it actually take to serve a http request? I would expect it to
be orders of magnitudes slower than calling os.listdir on a directory
containing hundreds of files.

Here on my Linux system there are 2000+ files in /usr/bin. Calling os.listdir
takes 1.5 milliseconds (warm cache):

$ python -m  timeit -s 'import os' 'os.listdir("/usr/bin")'
1000 loops, best of 3: 1.42 msec per loop

Converting those to upper case takes a further .5 milliseconds:

$ python -m  timeit -s 'import os' 'map(str.upper, os.listdir("/usr/bin"))'
1000 loops, best of 3: 1.98 msec per loop

Checking a string against that list takes .05 milliseconds:

$ python -m  timeit -s 'import os' \
  '"WHICH" in map(str.upper, os.listdir("/usr/bin"))'
1000 loops, best of 3: 2.03 msec per loop


Oscar



More information about the Python-list mailing list