Skip to content

Instantly share code, notes, and snippets.

@MSeifert04
Last active October 24, 2016 20:54
Show Gist options
  • Save MSeifert04/d2d4013093362c9e71c60b16ef53e355 to your computer and use it in GitHub Desktop.
Save MSeifert04/d2d4013093362c9e71c60b16ef53e355 to your computer and use it in GitHub Desktop.
import numpy as np
# =============================================================================
#
# Implementations
#
# Wrapped inside functions so the benchmark fails during setup not completly
# if library with this version or Python2 doesn't support this
#
# =============================================================================
def function_from_some_package():
import some_package
return some_package.function
def function_from_some_other_package():
import some_other_package
return some_package.function
def alternative():
def do_something(arr, mask=None, z=(2,2)):
...
return modified_arr
return do_something
FUNCS = {
'function_from_some_package': function_from_some_package,
'function_from_some_other_package': function_from_some_other_package,
'alternative': alternative,
}
# =============================================================================
#
# Wrapper for function call
#
# This is used so every function has the same overhead and identical
# behaviour
#
# =============================================================================
FUNCS_CALL_1 = {
'function_from_some_package': lambda f, arr: f(arr),
# Need to specify parameter x so the functions do identical things
'function_from_some_other_package': lambda f, arr: f(arr, x=(2, 2)),
'alternative': lambda f, arr: f(arr),
}
FUNCS_CALL_2 = {
# Impossible to do this with this function so let it fail
'function_from_some_package': lambda: None,
# The other two can handle that parameter.
'function_from_some_other_package': lambda f, arr, x: f(arr, x=x),
'alternative': lambda f, arr, x: f(arr, z=x),
}
# =============================================================================
# Fixed Parameters
# =============================================================================
arr = np.random.random((100, 100))
# =============================================================================
# Benchmark
# =============================================================================
class X:
params = ['function_from_some_package', 'function_from_some_other_package',
'alternative']
param_names = ('function')
def setup(self, func):
self.func = FUNCS[func]()
self.arr = arr
def time_noargs(self, func):
FUNCS_CALL_1[func](self.func, self.arr)
def time_witharg_small(self, func):
FUNCS_CALL_2[func](self.func, self.arr, (10, 10))
def time_witharg_big(self, func):
FUNCS_CALL_2[func](self.func, self.arr, (100, 100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment