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
from __future__ import division | |
from random import randint | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import pandas as pd | |
SAMPLES_PER_N_VALUE = 10000 | |
N_VALUES = [2**n for n in (range(10))] | |
sns.set() | |
def __has_mod(A, mod=2): | |
for i in range(len(A)): | |
if (A[i] % mod) == 0: | |
return i + 1 | |
return len(A) | |
data = [] | |
for N in N_VALUES: | |
for mod in (2, 3, 5, 10): | |
current_score = 0 | |
for _ in range(SAMPLES_PER_N_VALUE): | |
A = [randint(0, 1000000) for _ in range(N)] | |
current_score += __has_mod(A, mod) | |
data.append([ | |
N, | |
'mod={}'.format(mod), | |
current_score/SAMPLES_PER_N_VALUE, | |
]) | |
data=pd.DataFrame( | |
columns=[ | |
"N", | |
"Modulus", | |
"Avg runtime (over {} iterations)".format(SAMPLES_PER_N_VALUE), | |
], | |
data=data, | |
) | |
ax = sns.lineplot( | |
x="N", | |
y="Avg runtime (over {} iterations)".format(SAMPLES_PER_N_VALUE), | |
hue="Modulus", | |
style="Modulus", | |
data=data, | |
) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment