python统计函数被调用次数的实现

本文主要介绍了python如何统计函数被调用次数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、前言

每当做搜索任务或者时间复杂度任务相关时候,就会有统计函数被调用次数的需求。通常我们的做法是在函数前面定义一个变量,每循环调用一次,变量就加一,这不失为一种办法,那么有没有更高效的方法呢?

二、正文

第一种方法:

当然有,那就是python的独家专属操作—> 装饰器。
废话不多说,直接上例子:

class CallingCounter(object):     def __init__ (self, func):         self.func = func         self.count = 0     def __call__ (self, *args, **kwargs):         self.count += 1         return self.func(*args, **kwargs) @CallingCounter def test():     print('我被调用了') test() print(f'我被调用了{test.count}次')

如果是统计class中的函数被调用的次数,就把 装饰器 装在被调用函数的前面即可。

class CallingCounter(object):     def __init__ (self, func):         self.func = func         self.count = 0     def __call__ (self, *args, **kwargs):         self.count += 1         return self.func(*args, **kwargs) class Test:     @CallingCounter     def test():         print('我被调用了') for i in range(10):     Test.test() print(f'我被调用了{Test.test.count}次')

如果你的class中有很多的self用来调用函数,那么可能会报错,提示self无xxx属性or函数。这时候就要看第二种方法

第二种方法:

def call_counter(func):     def helper(*args, **kwargs):         helper.calls += 1         return func(*args, **kwargs)     helper.calls = 0     helper.__name__= func.__name__     return helper      # 下面是使用装饰器 @call_counter def f():     pass print(f.calls) for _ in range(10):     f() print(f.calls) # 0 10 # 这里是另一种调用方式 def f():     pass f = call_counter(f) print(f.calls) for _ in range(10):     f() print(f.calls) # 0 10

上面两种方法笔者都亲自做了测试,下面再介绍3个超简单的(狗头保命)

第3.1种方法(超简单)

calls = 0 def f():     global calls      calls  += 1     # do your func stuff here      ## 如果想统计class中的函数使用次数,同理

第3.2种方法(超简单)

class Test(object): def __init__(self): self.calls = 0 def f(self): self.calls += 1 # do your func stuff here 

第3.3种方法(超简单)

class Test(object):     calls = 0     def __init__(self):         pass     def f(self):         Test.calls += 1         # do your func stuff here # 调用方式 # Test().f() A = Test() for i in range(10):     A.f() print(A.calls) print(Test().calls) print(Test.calls)

三、小结

到此这篇关于python如何统计函数被调用次数的文章就介绍到这了,更多相关python统计函数被调用次数内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是python统计函数被调用次数的实现的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python