鱼C论坛

 找回密码
 立即注册
查看: 2766|回复: 11

[已解决]关于计算时间的代码返回值错误

[复制链接]
发表于 2020-2-20 20:42:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x



以下代码返回了一个非常小的数:1.9e-06,这是为什么?

  1. def calc(func):
  2.     import time
  3.     start = time.perf_counter()
  4.     func
  5.     return time.perf_counter() - start

  6. if __name__ == "__main__":
  7.     def test(return_value):
  8.         import time
  9.         time.sleep(10)
  10.         return return_value

  11.     print(calc(test("This is the return value.")))
复制代码
最佳答案
2020-2-20 20:57:26
一个账号 发表于 2020-2-20 20:56
这只支持传递一个参数
  1. def calc(func, *args, **kw):
  2.     import time
  3.     start = time.perf_counter()
  4.     print(func(*args, **kw))
  5.     return time.perf_counter()


  6. if __name__ == "__main__":
  7.     def test(return_value):
  8.         import time
  9.         time.sleep(10)
  10.         return return_value


  11.     print(calc(test, "This is the return value."))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-20 20:43:27 | 显示全部楼层
def calc(func):
    import time
    start = time.perf_counter()
    func()
    return time.perf_counter() - start
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 20:44:36 | 显示全部楼层
zltzlt 发表于 2020-2-20 20:43
def calc(func):
    import time
    start = time.perf_counter()


我不是那个意思,如果那样就无法向里面传递参数。

[b]print(calc(test("This is the return value.")))[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 20:45:57 | 显示全部楼层
  1. def calc(func):
  2.     import time
  3.     start = time.perf_counter()
  4.     func
  5.     return time.perf_counter()

  6. if __name__ == "__main__":
  7.     def test(return_value):
  8.         import time
  9.         time.sleep(10)
  10.         return return_value

  11.     print(calc(test("This is the return value.")))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 20:47:39 | 显示全部楼层

还是返回了一个非常小的数字:3.8e-06
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 20:47:49 | 显示全部楼层
那样是无法实现的,建议用装饰器:

  1. import time


  2. def calc(func):
  3.     def wrapper(*args, **kwargs):
  4.         start = time.perf_counter()
  5.         ret = func(*args, **kwargs)
  6.         end = time.perf_counter()
  7.         print(end - start)
  8.         return ret
  9.     return wrapper


  10. if __name__ == "__main__":
  11.     @calc
  12.     def test(return_value):
  13.         import time
  14.         time.sleep(1)
  15.         return return_value


  16.     print(test("This is the return value."))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 20:49:02 | 显示全部楼层
zltzlt 发表于 2020-2-20 20:47
那样是无法实现的,建议用装饰器:

那为什么那样实现不了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 20:50:06 | 显示全部楼层
一个账号 发表于 2020-2-20 20:49
那为什么那样实现不了?

既要返回 return_value,又要返回用时
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 20:51:16 | 显示全部楼层
或者你这样:

  1. def calc(func, ret):
  2.     import time
  3.     start = time.perf_counter()
  4.     print(func(ret))
  5.     return time.perf_counter()


  6. if __name__ == "__main__":
  7.     def test(return_value):
  8.         import time
  9.         time.sleep(10)
  10.         return return_value


  11.     print(calc(test, "This is the return value."))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 20:53:01 | 显示全部楼层
一个账号 发表于 2020-2-20 20:47
还是返回了一个非常小的数字:3.8e-06

我这里的结果
10.3842806
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 20:56:18 | 显示全部楼层

这只支持传递一个参数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 20:57:26 | 显示全部楼层    本楼为最佳答案   
一个账号 发表于 2020-2-20 20:56
这只支持传递一个参数
  1. def calc(func, *args, **kw):
  2.     import time
  3.     start = time.perf_counter()
  4.     print(func(*args, **kw))
  5.     return time.perf_counter()


  6. if __name__ == "__main__":
  7.     def test(return_value):
  8.         import time
  9.         time.sleep(10)
  10.         return return_value


  11.     print(calc(test, "This is the return value."))
复制代码

评分

参与人数 1鱼币 +2 收起 理由
一个账号 + 2 好的,谢谢

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-2 13:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表