Skip to content

Instantly share code, notes, and snippets.

@goodwillcoding
Last active September 5, 2016 21:45
Show Gist options
  • Save goodwillcoding/f0290c02e453c0b70c8f2a2b3f37d705 to your computer and use it in GitHub Desktop.
Save goodwillcoding/f0290c02e453c0b70c8f2a2b3f37d705 to your computer and use it in GitHub Desktop.
from timeit import Timer
# ........................................................................... #
FORBIDDEN_CHARS = \
('!',
'@',
'#',
'$',
'%',
'^',
'&',
'*',
'(',
')',
'_',
'+',
'-',
'=',
'[',
']',
'{',
'}',
';',
':',
"'",
'"',
'\\',
'|',
'~',
'`',
'<',
'>',
',',
'.',
'/',
'?',
)
TEST_STRING = "Lorem ipsum Sit qui non ex dolor dolore eu adipisicing exercitation culpa dolor eu reprehenderit deserunt Ut cillum minim dolor aute cillum sunt ex est labore sint in ut dolore Excepteur ullamco officia eu sint Excepteur ut mollit ut."
ITERATIONS = 1000000
REPEAT_TIMES = 3
# ........................................................................... #
# this is "SLOW", assuming you know what slow means and care
def two_level_lookup():
for char in FORBIDDEN_CHARS:
if char in TEST_STRING:
pass
# ........................................................................... #
def main():
timer = Timer("two_level_lookup()", "from __main__ import two_level_lookup")
print("go through " + str(ITERATIONS) + " iterations, " + str(REPEAT_TIMES) + " times")
results = timer.repeat(3, number=ITERATIONS)
print(results)
# calculate how long one iteration took, using first result
print("1 iteration took:" + '{0:f}'.format((results[0] / ITERATIONS)) + " of a second")
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #
if __name__=='__main__':
main()
$ python2 profile_chars.py
go through 1000000 iterations, 3 times
[17.295485973358154, 17.31608295440674, 17.370513916015625]
Iteration took:0.000017 of a second
$ python3 profile_chars.py
go through 1000000 iterations, 3 times
[5.974218010989716, 5.972314420010662, 5.972688858979382]
1 iteration took:0.000006 of a second
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment