我如何才能提高算法的速度,哪怕是最小的提高 [英] How could I improve the speed of my algorithm, even by the slightest amount

查看:49
本文介绍了我如何才能提高算法的速度,哪怕是最小的提高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能提高算法的速度,哪怕是最小的提高.由于我这样做的原因,我实际上丝毫不关心速度,但是我想知道如果我能改变什么.除了真正指出要更改的特定事物以外,我真正想知道的是如何知道有一种更有效的方法以及该方法如何工作.基本上,如果您想了解低级内容的工作方式,以便可以在较高级上编写更好的代码,应该遵循的路线.

How can I improve the speed of my algorithm, even by the slightest amount. For the reason I made this I don't actually care about speed in the slightest, but I'm wondering what I could change if I did. Even more than pointing out specific things to change, what I really want to know is how you know that there's a more efficient way and how that way works. Basically, what is the track to follow if you want to learn how the low level stuff works so that you can write better code at the higher level.

def decode_all(ob):
    if(type(ob)==list):
        for i in range(len(ob)):
            ob[i] = decode_all(ob[i])
    if(type(ob)==dict):
        for i in ob.copy():
            ob[i] = decode_all(ob[i])
            if(type(i)==bytes):
                ob[i.decode()] = ob[i]
                ob.pop(i)
    if(type(ob)==tuple):
        new_ob = []
        for i in ob:
            new_ob.append(decode_all(i))
        ob = tuple(new_ob)
    if(type(ob)==bytes):
        return ob.decode()
    return ob

推荐答案

修改后的代码:

def decode_all(ob):
    if isinstance(ob, list):
        return [decode_all(e) for e in ob]
    if isinstance(ob, dict):
        return {decode_all(k): decode_all(v) for k, v in ob.items()}
    if isinstance(ob, tuple):
        return tuple([decode_all(e) for e in ob])
    if isinstance(ob, bytes):
        return ob.decode()
    return ob

通常,我同意使用 if ... elif ... elif ... ,但是在这种情况下,立即返回结果会更简单.

Usually, I agree with the use of if ... elif ... elif ..., but in this case, it's simpler to just return the result right away.

一些注意事项:

  1. 没有副作用: ob 中的任何内容或其任何组件(无论深度如何)都没有被修改;所有新的集合和元素都将以一定的(少量)性能成本返还给它们.
  2. 使用 isinstance(),而不是 if type(x)== y (这是一个好习惯,对于基本类型在这里没有特别的优势).
  3. 使用列表和字典理解;简单得多,不必担心会更改原始的,本质上是pythonic.
  4. 无需处理dict键为字节的情况:请改用您自己的函数.
  5. tuple([x for list_x中的x]) tuple(x_list_x中的x)快,我最近了解到
  1. No side effects: nothing in ob or any of its components (at whatever depth) is modified; all new collections and elements are returned, at some (slight) performance cost for their allocation.
  2. Use isinstance(), not if type(x) == y (just a good habit, no particular advantage here with base types).
  3. Use list and dict comprehensions; far simpler, no fear of changing the original, and essentially pythonic.
  4. No need to handle the case where dict keys would be bytes: use your own function instead.
  5. tuple([x for x in list_]) is slightly faster than tuple(x for x in list_), I learned recently.

一些替代方法:

  1. 如果您喜欢 if ... elif ... :

def decode_all(ob):
    if isinstance(ob, list):
        ob = [decode_all(e) for e in ob]
    elif isinstance(ob, dict):
        ob = {decode_all(k): decode_all(v) for k, v in ob.items()}
    elif isinstance(ob, tuple):
        ob = tuple(decode_all(e) for e in ob)
    elif isinstance(ob, bytes):
        ob = ob.decode()
    return ob

  1. 最快(但有点不透明)

def _fun_bytes(ob):
    return ob.decode()

def _fun_list(ob):
    return [decode_all(e) for e in ob]

def _fun_dict(ob):
    return {decode_all(k): decode_all(v) for k, v in ob.items()}

def _fun_tuple(ob):
    return tuple([decode_all(e) for e in ob])

def _identity(ob):
    return ob

fun_dict = {
    bytes: _fun_bytes,
    list: _fun_list,
    dict: _fun_dict,
    tuple: _fun_tuple,
}

def decode_all(ob):
    return fun_dict.get(type(ob), _identity)(ob)

  1. 大多数可扩展的(您可以在代码库的各个区域中添加各种 .register ,例如以处理新类型):
  1. Most extensible (you can add various .register in various areas in your codebase, e.g. to handle new types):

从functools中的

from functools import singledispatch

@singledispatch
def decode_all(ob):
    return ob

@decode_all.register
def _(ob: bytes):
    return ob.decode()

@decode_all.register
def _(ob: list):
    return [decode_all(e) for e in ob]

@decode_all.register
def _(ob: dict):
    return {decode_all(k): decode_all(v) for k, v in ob.items()}

@decode_all.register
def _(ob: tuple):
    return tuple([decode_all(e) for e in ob])

这篇关于我如何才能提高算法的速度,哪怕是最小的提高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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