如果经常使用计算,我应该将其存储在变量中吗? [英] Should I store a calculation in a variable if it will be used a lot?

查看:90
本文介绍了如果经常使用计算,我应该将其存储在变量中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有一个功能,请检查list1是否是list2的子列表,哪个选项更好:

If I have a function to, for example, check if list1 is a sublist of list2, which option is better:

选项1:

def isSublist1(list1,list2):
  "This fuction checks if list1 is a sublist of list2."
  for i in range(len(list2)):
    part = list2[i:]  # part is a list with all the elements from i to the end of list2
    if len(part)<len(list1):
      return False
    if list1==part[:len(list1)]:  # if list1 is in the beginning of part 
      return True
  return False

或选项2:

def isSublist2(list1,list2):
  "This fuction checks if list1 is a sublist of list."
  for i in range(len(list2)):
    if len(list2[i:])<len(list1):
      return False
    if list1==list2[i:][:len(list1)]:  # if list1 is in the beginning of list2[i:] (part)
      return True
  return False

在选项1中,我使用名为part的变量来存储list2的一部分,但是,在选项2中,part不是变量,list2的部分是在需要时计算的.选项1更快吗?它会花费更多空间吗?

In option 1, I use a variable called part to store a section of list2 however, in option 2, part is not a variable, the section of list2 is calculated when it is needed. Is option 1 faster? Does it spend more space?

我的问题不是专门针对此功能,我知道还有其他方法可以实现此功能.

My problem is not with this function specifically, I know there are other ways to implement this function.

我想知道哪一个是循环中的最佳实践:使用变量避免多次计算相同的事物.答案是否取决于计算的复杂性和频率?

I would like to know which one is the best practice in a loop: using a variable to avoid calculating several times the same things or not. Does the answer depend on the complexity and frequency of the calculation?

推荐答案

存储本地更好,因为python的查找速度更快.甚至还可以在本地存储功能.

Storing local is better because the lookup that python does is faster. It even pays off to store functions locally.

最好通过定时对性能问题进行回答-您可以使用 timeit进行测量:

Performance questions are best answered by timing them - you can measure this using timeit:

import timeit

def noTempFunc():
    for _ in range(200):
        max([1,4,5,6])

def tempFunc():
    m = max
    for _ in range(200):
        m([1,4,5,6])


print(timeit.timeit(noTempFunc, number=1000))   #  0.055301458000030834
print(timeit.timeit(tempFunc, number=1000))     #  0.049811941999905684 : 11% faster

在这种情况下,全局上下文的max()只需要查找一次,并且在本地进行进一步的查找,基于这些数字,查找速度要快11%.

In this case the max() of the global context only has to be looked up once, and further lookups are done locally which is ~ 11% faster based on these numbers.

如果您多次使用本地m(),则可以付费.

It pays up if you use your local m() multiple times.

在您的情况下,缓存len_list1 = len(list1)是明智的-因为它使用了很多时间并且不会更改.

In your case it would be sensible to cache the len_list1 = len(list1) - as it is used lots of time and does not change.

要使其更具可读性,可以考虑:

To make it more readable, you can consider:

def isSublist(list1, list2):
    """Checks if list2 is a sublist of list1"""
    len_part = len(list2)  # reused inside the list comp, only "calulated" once
    return any( x == list2 for x in (list1[i:i+len_part] 
                                     for i in range(len(list1)-len_part+1) ))


print(isSublist([1,2,3,4],[1]))
print(isSublist([1,2,3,4],[2,3]))
print(isSublist([1,2,3,4],[1,2,4]))
print(isSublist([1,2,3,4],[1,2,3,4]))

输出:

True
True
False
True

查阅:

  • any() && How exactly does the python any() function work?
  • Split a python list into other "sublists" i.e smaller lists

您的更快版本以及已缓存的长度(基于 Scott Mermelstein 答案):

Your faster version with cached length as well (based on Scott Mermelstein answer):

def isSublist1a(list1,list2): # cached length as well
    l1 = len(list1)
    for i in range(len(list2)):
        part=list2[i:]
        if len(part)<l1:
            return False
        if list1==part[:l1]:
            return True
    return False

list1=list(range(1000))
list2=list(range(400,420))
import timeit
print(timeit.timeit('isSublist1(list2,list1)', globals=globals(),number=1000))
print(timeit.timeit('isSublist1a(list2,list1)', globals=globals(),number=1000))
print(timeit.timeit('isSublist2(list2,list1)', globals=globals(),number=1000))

提供(两次处决):

0.08652938600062043  # cached part
0.08017484299944044  # cached part + cached list1 lenght - slightly faster
0.15090413599955355  # non-cached version

0.8882850420004615   # cached part
0.8294611960000111   # cached part + cached list1 lenght - slightly faster
1.5524438030006422   # non-cached version

这篇关于如果经常使用计算,我应该将其存储在变量中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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