Which is better optimization?
The main function which actually returns the function object using above method would be called millions or billions of times, so need to do this intelligently. What could be the better way?
For e.g.
dict['str1'] = func1
dict['str2'] = func2
and so on..
def main_func(str):
return dict[str]
Or
def main_func(str):
if 'str1':
return func1
elif 'str2':
return func2
Which would be better..? if we have 50-60 such strings, and this process needs to be billions of times.
Storing function object inside dictionary, in function itself:-
def func1():
if dict.has_key('str1'):
dict['str1'] = func1
-- do something --
Which is better, this or the above one. This looks much cleaner.? But remember, these functions would be called many times so has_key function would also be called many times.
Thanks
Choose the dictionary.
The dictionary ...
I would suggest writing the solution using a dictionary first and seeing if the solution is fast enough for your needs. If so, great, you're done. If not, time it against the other way.
Consider a solution like this (which will return None
if the string is not found):
func_dict = {}
func_dict['str1'] = fun1
func_dict['str2'] = fun2
...
def function_lookup(func_string):
return func_dict.get(func_string)
Then, in your main, simply write function_lookup(whatever_string_variable)
to attempt a lookup for your function. This avoids the dictionary from being rebuilt every time function_lookup
is called.
The dictionary will be faster: it is approximately O(1), while the chain of if statements is O(n). To demonstrate, this script (make_test.py
) will output a python script that runs some peformance tests:
ifs = []
dict = []
for i in range(60):
string = 'str%d' % i
ifs.append(' %sif str == "%s": return %d' % ('el' if i else '', string, i))
dict.append('"%s": %d' % (string, i))
print 'dict = {', ','.join(dict), '}'
print 'def with_dict(str):'
print ' return dict[str]'
print 'def with_if(str):'
print '\n'.join(ifs)
print '''
import timeit
def test_dict():
for i in range(60):
with_dict("str%d" % i)
def test_if():
for i in range(60):
with_if("str%d" %i)
print 'dict:', timeit.timeit(test_dict, number=10000)
print 'if: ', timeit.timeit(test_if, number=10000)'''
Running it like python make_test.py | python
, gives me:
dict: 0.706176042557
if: 1.67383503914
That is, the if
version is more than 2 times slower than the dict
version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With