-
-
Save isermania/76bd664ddd28e7e19843dc3160341fe4 to your computer and use it in GitHub Desktop.
comparison of two functions which map the frequency of identical adjacent characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32 | |
Type "copyright", "credits" or "license()" for more information. | |
>>> import timeit, itertools, random, string, statistics | |
>>> | |
>>> def summarize(s): | |
return ''.join(str(sum(1 for _ in i[1])) + i[0] for i in itertools.groupby(s)) | |
>>> def freq_map(s): | |
num = 0 # number of adjacent, identical characters | |
curr = s[0] # current character being processed | |
result = '' # result of function | |
for i in range(len(s)): | |
if s[i] == curr: | |
num += 1 | |
else: | |
result += str(num) + curr | |
curr = s[i] | |
num = 1 | |
result += str(num) + curr | |
return result | |
>>> stimes, ftimes = [],[] | |
>>> for i in range(5): | |
s = ''.join(random.choices(string.ascii_uppercase + string.digits, k=20)) | |
stimes.append(timeit.timeit("[summarize('{0}')]".format(s), globals=globals())) | |
ftimes.append(timeit.timeit("[freq_map('{0}')]".format(s), globals=globals())) | |
>>> stimes | |
[20.988593877864446, 22.52263322176168, 20.372410243008744, 24.40531525133622, 22.33915113723708] | |
>>> ftimes | |
[12.049683384776813, 10.223272452521542, 13.329730250382795, 16.72326475475097, 11.291220080838343] | |
>>> statistics.mean(stimes) | |
22.125620746241633 | |
>>> statistics.mean(ftimes) | |
12.723434184654092 | |
>>> 12.723434184654092 / 22.125620746241633 | |
0.5750543377100666 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment