Python分析,导入(尤其是__init__)似乎是花费最多的时间

吉姆·巴斯奎特

我有一个脚本,运行似乎很慢,并且使用cProfile(和可视化工具KCacheGrind进行了概要分析

似乎占用了几乎90%的运行时间的是导入顺序,尤其是_ _ init _ _.py文件的运行...

这是KCacheGrind输出的屏幕截图(很抱歉,附加图片...)

在此处输入图片说明

我对导入序列在python中的工作方式不是很熟悉,所以也许我感到有些困惑...我也将_ _ init _ _.py文件放置在我的所有自定义软件包中,不确定是否应该这样做。

无论如何,如果有人有任何暗示,不胜感激!


EDIT: additional picture when function are sorted by self:

在此处输入图片说明


EDIT2:

here the code attached, for more clarity for the answerers:

from strategy.strategies.gradient_stop_and_target import make_one_trade

from datetime import timedelta, datetime
import pandas as pd
from data.db import get_df, mongo_read_only, save_one, mongo_read_write, save_many
from data.get import get_symbols

from strategy.trades import make_trade, make_mae, get_prices, get_signals, \
    get_prices_subset
#from profilehooks import profile


mongo = mongo_read_only()


dollar_stop = 200
dollar_target = 400
period_change = 3


signal = get_df(mongo.signals.signals, strategy = {'$regex' : '^indicators_group'}).iloc[0]


symbol = get_symbols(mongo, description = signal['symbol'])[0]


prices = get_prices(
    signal['datetime'], 
    signal['datetime'].replace(hour = 23, minute = 59),
    symbol,
    mongo)


make_one_trade(
    signal, 
    prices, 
    symbol,             
    dollar_stop,
    dollar_target,
    period_change)

The function get_prices simply get data from a mongo db database, and make_one_trade does simple calculation with pandas. This never poses problem anywhere else in my project.


EDIT3:

Here the Kcache grind screen when i select 'detect cycle' option in View tab:

在此处输入图片说明

Could that actually mean that there are indeed circular imports in my self created packages that takes all that time to resolve?

Martijn Pieters

No. You are conflating cumulative time with time spent in the top-level code of the __init__.py file itself. The top-level code calls other methods, and those together take a lot of time.

Look at the self column instead to find where all that time is being spent. Also see What is the difference between tottime and cumtime in a python script profiled with cProfile?, the incl. column is the cumulative time, self is the total time.

I'd just filter out all the <frozen importlib.*> entries; the Python project has already made sure those paths are optimised.

但是,您的第二个屏幕快照确实表明,在性能分析运行中,您自己忙碌的所有Python代码都正在加载字节码以供模块导入(marshal模块提供了Python字节码序列化实现)。Python程序只执行了导入模块的工作,而没有做其他工作,或者正在使用某种形式的动态导入来装载大量的模块,或者忽略了正常的模块缓存并反复重新装载了相同的模块。

您可以使用Python 3.7的新-X importtime命令行开关来分析导入时间,也可以使用专用的import-profiler来找出为什么导入需要这么长时间。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

__init__函数似乎是在定义而不是实例上执行

Python __init__文件无法导入模块

Python - __init__ 是否在导入后执行?

导入模块的方案语法是什么(尤其是贵族)?

分析:为什么查询时间可能会花费这么长时间,而且成本似乎很低?

导入__init__

Python分析:在函数的每一行上花费的时间

Pytest 在测试后花费最多时间?

Postgres 漏斗分析(花费的时间)

Python导入有效,但是使用导入项失败。使用Django,但这似乎是Python的新手问题

python 语法有问题,尤其是在 if 检查上

在 python 中获取画布坐标 - 尤其是对于 mac

如何使用 __init__ 在 python 中导入顶级文件

已安装 Python 包,但 __init__ 中的内容不可导入

导入时未调用Python自定义__init__

Python导入提供了即使在__init__之后也找不到的packge

python linprog,lp似乎是无界的

在 Flask 中从 __init__ 导入

按时间间隔中花费的最多时间标记行

Java的面向接口编程我将用它所有的时间,尤其是对于收藏

一段时间后性能会下降,尤其是在设备处于静止状态时

为多个时区的用户指定时间(尤其是美国/加拿大)

LSTM:了解时间步,样本和功能,尤其是在重塑和input_shape中的用途

蜂巢分析查询花费大量时间

Python覆盖__init__

Python重载__init__

Python - __init__ 参数

记录导入模块所花费的时间

Python 3如何知道如何腌制扩展类型,尤其是Numpy数组?