public
Last active

  • Download Gist
profile_select_random_element.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
"""Measure relative performance of answers to [1].
 
[1] http://stackoverflow.com/questions/1456617
"""
import linecache
import random
from timeit import default_timer
 
 
WORDS_FILENAME = "/etc/dictionaries-common/words"
 
 
def measure(func):
measure.func_to_measure.append(func)
return func
measure.func_to_measure = []
 
 
@measure
def dcrosta():
words = [line.strip() for line in open(WORDS_FILENAME)]
return random.choice(words)
 
 
@measure
def dcrosta_no_strip():
words = [line for line in open(WORDS_FILENAME)]
return random.choice(words)
 
 
def select_random_line(filename):
selection = None
count = 0
for line in file(filename, "r"):
if random.randint(0, count) == 0:
selection = line.strip()
count = count + 1
return selection
 
 
@measure
def mark_ransom():
return select_random_line(WORDS_FILENAME)
 
 
def select_random_line_no_strip(filename):
selection = None
count = 0
for line in file(filename, "r"):
if random.randint(0, count) == 0:
selection = line
count = count + 1
return selection
 
 
@measure
def mark_ransom_no_strip():
return select_random_line_no_strip(WORDS_FILENAME)
 
 
def choose_from(iterable):
"""Choose a random element from a finite `iterable`.
 
If `iterable` is a sequence then use `random.choice()` for efficiency.
 
Return tuple (random element, total number of elements)
"""
selection, i = None, None
for i, item in enumerate(iterable):
if random.randint(0, i) == 0:
selection = item
 
return selection, (i+1 if i is not None else 0)
 
 
@measure
def mark_ransom_choose_from():
return choose_from(open(WORDS_FILENAME))
 
 
@measure
def nadia():
global total_num_lines
total_num_lines = sum(1 for _ in open(WORDS_FILENAME))
 
line_number = random.randint(0, total_num_lines)
return linecache.getline(WORDS_FILENAME, line_number)
 
 
@measure
def nadia_known_num_lines():
line_number = random.randint(0, total_num_lines)
return linecache.getline(WORDS_FILENAME, line_number)
 
 
@measure
def jfs():
return random.choice(list(open(WORDS_FILENAME)))
 
 
def timef(func, number=1000, timer=default_timer):
"""Return number of seconds it takes to execute `func()`."""
start = timer()
for _ in range(number):
func()
return (timer() - start) / number
 
 
def main():
# measure time
times = dict((f.__name__, timef(f, number=10))
for f in measure.func_to_measure)
 
# print from fastest to slowest
maxname_len = max(map(len, times))
last = None
for name in sorted(times, key=times.__getitem__):
print "%s %4.2g seconds %.2f" % (name.ljust(maxname_len), times[name],
last and times[name] / last or 1)
last = times[name]
 
 
if __name__ == "__main__":
main()
 
# nadia_known_num_lines 9.6e-06 seconds 1.00
# nadia 0.056 seconds 5843.51
# jfs 0.062 seconds 1.10
# dcrosta_no_strip 0.091 seconds 1.48
# dcrosta 0.13 seconds 1.41
# mark_ransom_no_strip 0.66 seconds 5.10
# mark_ransom_choose_from 0.67 seconds 1.02
# mark_ransom 0.69 seconds 1.04

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.