python - Better optimization technique using if/else or dictionary -


Which is better customization?

  • A series of if / else statement that gives 'string' the appropriate function for it (around 40-50 if / other statement)
  • key-value pair A dictionary to maintain A key function in the form of the string, the value as the function object, and the search for the function object and returning.

    What is the best way to function the function that actually uses the above method, millions or billions times, so it needs to be done wisely?

    For example

      dict ['str1'] = func1 dict ['str2'] = func2 and so on .. def main_func (str): return dict [ [Str1]: return func1 elif 'str2': return func2    

    or

      def main_func (str) Pre> 

    Which would be better ..? If we have 50-60 such strings, and this process needs to be billions of times.

    Function Object inside the function within the dictionary, in the function itself: -

      def func1 (): if dict.has_key ('str1 '): Dict [' str1 '] = func1 - do something -   

    Which is better, this one or the one above is very clear. But remember, these functions will be called many times so that the key function will be called many times.

    Thanks

    Choose dictionary

    • Python
    • requires less boilerplate code
    • if (second) is compared to the complexity of linear o (n) 1) Complexity, then
    • is not guilty of premature judgment (we have no reason to believe inadequate reason that it is a less efficient method by a large margin). >

      I used to first add a dictionary Science suggests writing; and if the solution to your needs fast enough. If so, well, if you have not, then it is time for the other side.

      Then, in your main, To try a lookup for your work, type function_lookup (whatever_string_variable) . It is called every time the function_lookup is avoided by dictionary dictionary.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -