【问题标题】:Can I run multiple modules with -m flag?我可以使用 -m 标志运行多个模块吗?
【发布时间】:2014-08-13 14:16:39
【问题描述】:

我想profile 我的python 代码尽可能多的参数: 1.时间(目前用memory_profiler) 2.内存(目前用cProfileprofile) 3. I/O - 每秒读/写字节数(没找到)

(据我所知)最简单的做法是在上面的模块上使用 -m 标志(例如 python -m cProfile [-o output_file] [-s sort_order] myscript.py

那么,如何为 memory_profiler 和 cProfile/profile 模块使用 -m 标志?

【问题讨论】:

    标签: python profile memory-profiling cprofile


    【解决方案1】:

    我不相信您可以将 -m 标志与多个模块一起使用。但是,在脚本中使用 cProfile 一点也不难:

    import cProfile, pstats
    
    profiler = cProfile.Profile()    # create profiler
    profiler.enable()                # start profiling
    
    # do stuff
    
    profiler.disable()               # end profiling
    
    with open('profile.txt', 'w') as fo: # open a file to print the stats
        pstat_profile = pstats.Stats(profiler, stream=fo) # create a pstats object from the profile bound to a file stream
        pstat_profile = print_stats() # print stats to a file
    

    这将为您提供有条理的计时统计信息打印输出。现在您可以使用 -m 为 memory_profiler 运行这个修改后的脚本,并同时拥有两者。

    我不知道有任何基于 Python 的工具用于监控访问。但是,Google 搜索“测量 IO 速度”会在各处搜索到,因此您应该能够找到您使用的第三方实用程序(或一些内置工具 like DD,如果您使用的是 Linux)可用于在启动脚本时跟踪 IO 性能。

    您最终将在 IO 记录器内的内存分析器下运行时间分析脚本,但我认为它们应该可以很好地协同工作,并且它们的交互应该很容易被发现。例如,您在脚本内启动时间分析器将在内存配置文件中显示为内存分配,但您会知道可以忽略它们。

    【讨论】:

    • 谢谢。关于 IO 速度的测量 - 我的首要任务是找到这种不是第三方的分析器(如果是第三方,我需要同步不同的分析器结果)
    • @TalBarda 不幸的是,我认为您无法找到它。我只是不认为有任何原生 Python 工具可以测量这类事情。
    猜你喜欢
    • 2023-03-30
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    相关资源
    最近更新 更多