简体   繁体   English

如何组合两个几乎相同的功能但有效地返回不同的 output

[英]How can I combine two almost same functions but return different output efficiently

I have two functions, which are almost same, in a main function.我在主 function 中有两个几乎相同的功能。 For a simplified example,举一个简化的例子,

def func1(a):
    for i in range(999):
       a += np.exp(-i)     # some massive calculation
    return a

def func2(a):
    b = 0
    for i in range(999):
       a += np.exp(-i)     # same massive calculation
       b += np.exp(-(a+b)) # additional massive calculation 
    return a, b

def main():
    a = 1
    for _ in range(9999):
        a = func1(a)
    print(a)
    a = 0
    c = 0
    for _ in range(9999):
        a, b = func2(a)
        c +=b
    print(a, c)

I know I could have combined func1 and func2 as我知道我可以将func1func2组合为

def func3(a, return_b):
    b = 0
    for i in range(999):
       a += np.exp(-i) 
       if return_b:        # add if statement
           b += np.exp(-(a+b))            
    return a,b  

and the main function becomes和主要的 function 变成

def main():
    a = 1
    for _ in range(9999):
        a,_ = func3(a, False)
    print(a)
    a = 0
    c = 0
    for _ in range(9999):
        a, b = func3(a, True)
        c +=b
    print(a, c)

But I think it's really expensive to add if statement in a large number of loops.但是我认为在大量循环中添加 if 语句确实很昂贵。 And I would rather repeat func1 in func2 and leave both functions in the code.我宁愿在func2中重复func1并将这两个函数留在代码中。 I wonder what the best way is to combine func1 and func2 together by considering the running time and code re-use efficiency.考虑到运行时间和代码重用效率,我想知道将func1func2结合在一起的最佳方法是什么。

This code below is less expensive than if inside for loop and as you wanted it still is in one function.下面的这段代码比 if inside for 循环更便宜,而且你想要它仍然在一个 function 中。

def func3(a, return_b):
    b = 0
    #checks only once
    if return_b:
        for i in range(999):
            a += np.exp(-i) 
            b += np.exp(-(a+b))
    else:
        for i in range(999):
            a += np.exp(-i) 
    return a,b 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何组合两个采用相同参数的函数? - How can I combine two functions that take the same arguments? 我可以结合这两个函数并获得相同的输出吗? - Could I combine these two functions and get the same output? 我可以在python中将这两个几乎相同的函数合二为一吗? - Can I turn these two almost identical functions into one in python? 如何让我的 function 使用两个辅助函数来返回所需的 output? - How can I make my function use two helper functions to return the desired output? 我可以同时从两个函数返回吗? - Can I return from two functions at the same time? 如何对3个不同的函数进行多线程处理,这些函数返回相同的值并选择最快的函数 - How can I multi-thread 3 different functions that return the same value and pick the fastest 如何使用不同的聚合函数有效地聚合同一列? - How to efficiently aggregate the same column using different aggregate functions? 如何在 python 中有效地组合 function 返回的数据帧? - How can I combine the dataframes returned by a function efficiently in python? 如何有效地合并这两个数据集? - How can I efficiently merge these two datasets? Python 3、如何同时运行两个函数? - In Python 3, how can I run two functions at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM