如何确定函数的 CPU 和内存成本?

发布于 2024-12-28 09:30:28 字数 412 浏览 1 评论 0原文

有人可以建议什么是最佳实践或合适的库来确定:

  1. Python 函数执行期间使用的CPU 周期数量?
  2. 同一 Python 函数使用的内存量?

我看过孔雀鱼meliae,但仍然无法精细到功能级别?我错过了什么吗?

更新 提出这个问题是为了解决一个具体的情况,即场景是我们有一组分布式任务运行在云实例上,现在我们需要重新组织任务在集群内正确的实例类型上的放置,例如例如,高内存消耗的功能任务将被放置在更大的内存实例上,等等。当我指的是任务(celery-tasks)时,这些只是简单的函数,我们现在需要分析它们的执行使用情况。

谢谢。

Can someone suggest what would be the best practice or a suitable library to determine:

  1. Number of CPU cycles used during execution of a Python function?
  2. Amount of Memory used by the same Python function?

I had looked at guppy and meliae, but still can't get granular to the function level? Am I missing something?

UPDATE
The need for asking this question is to solve a specific situation which is, the scenario is that we have a set of distributed tasks running on cloud instances, and now we need to reorganize the placement of tasks on right instance types withing the cluster, for example high memory consuming functional tasks would be placed on larger memory instances and so on. When I mean tasks (celery-tasks), these are nothing but plain functions for which we need to now profile their execution usage.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听,心雨的声音 2025-01-04 09:30:28

您可能想研究一下适用于 Python 的 CPU 分析器
http://docs.python.org/library/profile.html

cProfile.run(command[, filename]) 的示例输出

  2706 function calls (2004 primitive calls) in 4.504 CPU seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     2    0.006    0.003    0.953    0.477 pobject.py:75(save_objects)
  43/3    0.533    0.012    0.749    0.250 pobject.py:99(evaluate)
...

此外,内存 也需要分析器:
开源分析器:PySizer厚重

You may want to look into a CPU profiler for Python:
http://docs.python.org/library/profile.html

Example output of cProfile.run(command[, filename])

  2706 function calls (2004 primitive calls) in 4.504 CPU seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     2    0.006    0.003    0.953    0.477 pobject.py:75(save_objects)
  43/3    0.533    0.012    0.749    0.250 pobject.py:99(evaluate)
...

Also, memory needs a profiler too:
open source profilers: PySizer and Heapy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文