Skip to content

Instantly share code, notes, and snippets.

@zed
Last active March 20, 2018 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zed/7fbeb0c33079ab3c5cac78a39fc95a1d to your computer and use it in GitHub Desktop.
Save zed/7fbeb0c33079ab3c5cac78a39fc95a1d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
x = [10**3, 10**4, 10**5, 10**6]
# y = [18.1e-6, 181e-6, 1.79e-3, 18.2e-3] # 1 x - 7.744
# y = np.array(x)**2 # 2 x + 3.553e-15
y = [34.9e-6, 576e-6, 37.2e-3, 4.92] # 1.726 x - 9.874
z = np.polyfit(np.log10(x), np.log10(y), 1)
p = np.poly1d(z)
print(p)
xp = np.linspace(3, 6, 100)
_ = plt.plot(np.log10(x), np.log10(y), '.', xp, p(xp), '-')
plt.show()
#!/usr/bin/env python
"""Compare performance linear vs. quadratic algorithm."""
def slices_linear(s, step=10):
for i in range(0, len(s), step):
chunk = s[i:i+step] # O(1)
def slices_quadratic(s, step=10):
for i in range(0, len(s), step):
chunk = s[:step]
s = s[step:] # O(n)
if __name__ == '__main__':
# https://gist.github.com/4276624
from reporttime import get_functions_with_prefix, measure
funcs = get_functions_with_prefix('slices_')
for n in range(3, 7):
measure(funcs, args=["a" * 10**n], comment='10**' + str(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment