Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
from benchy.api import Benchmark, BenchmarkSuite, BenchmarkRunner
import matplotlib.pyplot as plt
common_setup = """
import numpy
X = numpy.random.uniform(1,5,(1000,))
"""
statement = "cosine_distances(X, X)"
setup_bk1 = common_setup + """
import scipy.spatial.distance as ssd
X = X.reshape(-1,1)
def cosine_distances(X, Y):
return 1. - ssd.cdist(X, Y, 'cosine')
"""
benchmark1 = Benchmark(statement, setup_bk1, name="scipy.spatial 0.8.0")
setup_bk2 = common_setup + """
from sklearn.metrics.pairwise import cosine_similarity as cosine_distances
"""
benchmark2 = Benchmark(statement, setup_bk2, name="sklearn 0.13.1")
setup_bk3 = common_setup + """
from nltk import cluster
def cosine_distances(X, Y):
return 1. - cluster.util.cosine_distance(X, Y)
"""
benchmark3 = Benchmark(statement, setup_bk3, name="nltk.cluster")
setup_bk4 = common_setup + """
import numpy, math
def cosine_distances(X, Y):
return 1. - numpy.dot(X, Y) / (math.sqrt(numpy.dot(X, X)) *
math.sqrt(numpy.dot(Y, Y)))
"""
benchmark4 = Benchmark(statement, setup_bk4, name="numpy")
suite = BenchmarkSuite()
suite.append(benchmark1)
suite.append(benchmark2)
suite.append(benchmark3)
suite.append(benchmark4)
runner = BenchmarkRunner(suite, '.', 'Cosine benchmarks')
n_benchs, results = runner.run()
fig = runner.plot_relative(results, horizontal=True, logy=True)
plt.savefig('%s_r.png' % runner.name.replace(' ', '_')) # bbox_inches='tight')
runner.plot_absolute(results, horizontal=False, logy=True)
plt.savefig('%s.png' % runner.name.replace(' ', '_')) # bbox_inches='tight')
rst_text = runner.to_rst(results, runner.name.replace(' ', '_') + 'png',
runner.name.replace(' ', '_') + '_r.png')
with open('bench.rst', 'w') as f:
f.write(rst_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment