Which is better optimization?
- A series of if/else statement which receives the 'string' returns the appropriate function for it. (Around 40-50 if/else statements).
- A dictionary maintaining the key-value pair. key as strings, and values as the function objects, and one main function to search and return the function object.
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