Skip to content

Instantly share code, notes, and snippets.

@pschwede
Created August 1, 2012 08:57
Show Gist options
  • Save pschwede/3225185 to your computer and use it in GitHub Desktop.
Save pschwede/3225185 to your computer and use it in GitHub Desktop.
Timing the search for a string in a list.
#!/usr/bin/env python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import re
import timeit
my_list = ['abc-123', 'def-456', 'ghi-789', 'abc456', 'abc', 'abd']
re_abc = re.compile("^abc$")
def re_check():
"""compiles a regular expression and checks for matches"""
return [i for i in my_list if re.match('^abc$', i)]
def simple_check():
"""AFAIK only works for ascii strings in Python < 3.0"""
return [i for i in my_list if i == 'abc']
def re_precompiled():
"""uses precompiled regular expression and checks for matches"""
return [i for i in my_list if re_abc.match(i)]
def contains_check():
"""looks for existing entry in my_list. returns only one element"""
return ["abc"] if "abc" in my_list else []
def count_check():
"""looks for existing entry in my_list. returns all elements"""
return ["abc"]*my_list.count("abc")
if __name__ == "__main__":
# running checks
for check in [i for i in dir() \
if "<type 'function'>" == str(type(eval(i)))]:
t = eval("timeit.Timer(%s)" % check)
print '%s result >>' % check, eval("%s()" % check)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000) / 100000)
# Results:
# ==================================
#~ contains_check result >> ['abc']
#~ 0.42 usec/pass
#~ count_check result >> ['abc']
#~ 0.74 usec/pass
#~ re_check result >> ['abc']
#~ 12.43 usec/pass
#~ re_precompiled result >> ['abc']
#~ 4.47 usec/pass
#~ simple_check result >> ['abc']
#~ 0.86 usec/pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment