使用if/else或Dictionary的更好的优化技术 [英] Better optimization technique using if/else or dictionary

查看:117
本文介绍了使用if/else或Dictionary的更好的优化技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个是更好的优化?

  • 接收字符串"的一系列if/else语句将为其返回适当的函数.(大约40-50 if/else语句).
  • 维护键值对的字典.键是字符串,值是函数对象,还有一个主要函数来搜索并返回该函数对象.

使用上述方法实际返回函数对象的主要函数将被称为数百万或数十亿次,因此需要智能地执行此操作.有什么更好的方法?

例如

  dict ['str1'] = func1dict ['str2'] = func2等等..def main_func(str):返回字典[str] 

  def main_func(str):如果为"str1":返回func1elif'str2':返回func2 

哪个会更好..?如果我们有50-60个这样的字符串,那么这个过程需要数十亿次.

将函数对象存储在字典中,即函数本身:-

  def func1():如果dict.has_key('str1'):dict ['str1'] = func1-  做一点事  - 

这个或以上哪个更好.这看起来更干净.但是请记住,这些函数将被多次调用,所以has_key函数也将被多次调用.

谢谢

解决方案

选择字典.

字典...

  • 是内置的
  • 是pythonic
  • 需要更少的样板代码
  • 与li-else线性O(n)复杂度相比,
  • 具有O(1)复杂度
  • 对过早的悲观无罪(我们没有足够的理由相信,如果不概要分析它是效率低下的方法,则大有作为)

我建议先使用字典编写解决方案,然后查看解决方案是否足够快以满足您的需求.如果是这样,那就太好了,您就完成了.如果没有,则以相反的方式计时.<​​/p>

考虑这样的解决方案(如果找不到该字符串,则返回 None ):

  func_dict = {}func_dict ['str1'] = fun1func_dict ['str2'] = fun2...def function_lookup(func_string):返回func_dict.get(func_string) 

然后,在您的主体中,只需编写 function_lookup(whatever_string_variable)来尝试查找您的函数.这样可以避免每次调用 function_lookup 时都重新构建字典.

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

解决方案

Choose the dictionary.

The dictionary ...

  • is built-in
  • is pythonic
  • requires less boilerplate code
  • has O(1) complexity, compared to the if-else linear O(n) complexity
  • isn't guilty of premature pessimization (we have insufficient reason to believe without profiling that it is a less efficient method by a large margin)

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.

这篇关于使用if/else或Dictionary的更好的优化技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆