Skip to content

Instantly share code, notes, and snippets.

@mplanchard
Created January 3, 2019 14:34
Show Gist options
  • Save mplanchard/00d637708b63ddf5fecd8cf64e035a00 to your computer and use it in GitHub Desktop.
Save mplanchard/00d637708b63ddf5fecd8cf64e035a00 to your computer and use it in GitHub Desktop.
Timing of Static Methods vs. Inline Functions
"""
========================================================================
Timing Static Methods vs. Inline Functions
========================================================================
A simple test to compare the overhead of calling static functions vs.
defining and calling inline functions.
At least in this experiment, static methods are ~15-20% faster. This
is of course not even considering their improvements in testability
and compasability.
------------------------------------------------------------------------
Results on MacOS 10.14.2, 3.1 GHz i7
------------------------------------------------------------------------
Python 2.7.15
*************
Average time to call inline functions over 1000000 iterations:
7.55923986435e-07
Average time to call static methods over 1000000 iterations:
6.24632835388e-07
Percent Difference: 17.37%
Python 3.7.1
************
Average time to call inline functions over 1000000 iterations:
1.0708249599999999e-06
Average time to call static methods over 1000000 iterations:
8.65215882e-07
Percent Difference: 19.20%
"""
from __future__ import division, print_function
from timeit import timeit
ITERATIONS = int(1e6)
class StaticFunctions(object):
@staticmethod
def a():
return 'a'
@staticmethod
def b():
return 'b'
@staticmethod
def c():
return 'c'
@staticmethod
def d():
return 'd'
@staticmethod
def e():
return 'e'
@staticmethod
def f():
return 'f'
def call_static_functions():
return (
StaticFunctions.a()
+ StaticFunctions.b()
+ StaticFunctions.c()
+ StaticFunctions.d()
+ StaticFunctions.e()
+ StaticFunctions.f()
)
def define_and_call_inline_functions():
def a():
return 'a'
def b():
return 'b'
def c():
return 'c'
def d():
return 'd'
def e():
return 'e'
def f():
return 'f'
return a() + b() + c() + d() + e() + f()
print(
'Average time to call inline functions over {} iterations:'.format(
ITERATIONS,
)
)
inline_total = timeit(
'define_and_call_inline_functions()',
setup='from __main__ import define_and_call_inline_functions',
number=ITERATIONS
)
inline_avg = inline_total / ITERATIONS
print(' {}'.format(inline_avg))
print(
'Average time to call static methods over {} iterations:'.format(
ITERATIONS,
)
)
static_total = timeit(
'call_static_functions()',
setup='from __main__ import call_static_functions',
number=ITERATIONS
)
static_avg = static_total / ITERATIONS
print(' {}'.format(static_avg))
higher = max(inline_total, static_total)
lower = min(inline_total, static_total)
pcent_dif = (higher - lower) / higher
print()
print('Percent Difference: {:.2%}'.format(pcent_dif))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment