%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ran_t = np.arange(0.0, 1.0, 1/44100, dtype=np.float32)
ran_t
def g(freq, t) -> np.int16:
return np.sin(freq * 2 * np.pi * t)
vectorized_secs = %timeit -o Hz_17 = g(17, ran_t)
vectorized_secs.best
Hz_17_16bit = np.int16(np.float32(32767) * Hz_17)
Hz_17_16bit
ran_t.dtype, Hz_17.dtype, Hz_17_16bit.dtype
plt.plot(Hz_17_16bit);
loop_secs = %timeit -o for t in ran_t: g(7, t)
loop_secs.best
def comparison(slower, faster):
'slower, faster: seconds'
ratio = slower/faster
order_of_magnitude = np.log10(ratio)
return ratio, order_of_magnitude
print("With record_history disabled:\n"
"Vectorized approach is %d times (about %.1f orders of magnitude) faster"
% comparison(slower=loop_secs.best, faster=vectorized_secs.best))
record_history
-decorated version of the same functionfrom log_calls import record_history
@record_history()
def f(freq, t):
return np.sin(freq * 2 * np.pi * t)
record_history
disabledf.record_history_settings.enabled = False
vectorized_secs_rh_disabled = %timeit -o Hz_17 = f(17, ran_t)
vectorized_secs_rh_disabled.best
loop_secs_rh_disabled = %timeit -o for t in ran_t: f(7, t)
loop_secs_rh_disabled.best
print("With record_history disabled:\n"
"Vectorized approach is %d times (about %.1f orders of magnitude) faster"
% comparison(slower=loop_secs_rh_disabled.best, faster=vectorized_secs_rh_disabled.best))
record_history
enabledf.record_history_settings.enabled = True
f.stats.clear_history()
vectorized_secs_rh_enabled = %timeit -o f.stats.clear_history(); Hz_17 = f(17, ran_t)
vectorized_secs_rh_enabled.best
len(f.stats.history)
def size_of_t_for_row(row):
return f.stats.history[row].argvals[1].size
size_of_t_for_row(0)
f.stats.history[0].retval.size
f.stats.history
f.stats.clear_history()
loop_secs_rh_enabled = %timeit -o for t in ran_t: f(7, t); f.stats.clear_history()
loop_secs_rh_enabled.best
print("With record_history enabled:\n"
"Vectorized approach is %d times (about %.1f orders of magnitude) faster"
% comparison(slower=loop_secs_rh_enabled.best, faster=vectorized_secs_rh_enabled.best))
So recording history slows things down by another order of magnitude :|
f.stats.clear_history()
for t in ran_t: f(7, t)
df = f.stats.history_as_DataFrame
from IPython.display import display
display(df.head())
display(df.tail())
df[:5]
len(f.stats.history)
df[['t', 'retval']].head()
plt.plot(df.t, df.retval);