可以逐行分析Julia代码吗? [英] Line-by-line profiling of Julia code possible?

查看:63
本文介绍了可以逐行分析Julia代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Julia函数,这些函数有几百行,我想对其进行概要分析,以便随后可以进行代码优化.

I have some Julia functions that are several hundreds of lines long that I would like to profile so that I can then work on optimizing the code.

我知道 BenchmarkTools 软件包,该软件包允许使用 @btime @benchmark 来测量函数的整体执行时间和内存消耗.代码>.但是这些功能并没有告诉我瓶颈在哪里.因此,我的第一步必须是使用某种工具来识别代码的哪些部分运行缓慢.

I am aware of the BenchmarkTools package which allows the overall execution time and memory consumption of a function to be measured using @btime or @benchmark. But those functions tell me nothing about where inside the functions the bottlenecks are. So my first step would have to be using some tool to identify which parts of the code are slow.

例如,在Matlab中,有一个非常好的内置分析器,该分析器运行脚本/函数,然后报告在代码的每一行上花费的时间.同样,在Python中,有一个名为 line_profiler 的模块,该模块可以逐行生成报告,显示在函数的每一行上花费了多少时间.

In Matlab for instance there is a very nice built-in profiler which runs a script/function and then reports the time spent on every line of the code. Similarly in Python there is a module called line_profiler which can produce a line-by-line report showing how much time was spent on every single line of a function.

我正在寻找的只是一份逐行报告,其中显示了每行代码所花费的总时间以及某段特定代码被调用了多少次.

What I’m looking for is simply a line-by-line report showing the total time spent on each line of code and how many times a particular piece of code was called.

Julia中有这样的功能吗?内置或通过某些第三方程序包.

Is there such a functionality in Julia? Either built-in or via some third-party package.

推荐答案

有一个一章,其中包含所有必要的信息.

There is a Profiling chapter in Julia docs with all the necessary info.

此外,您可以使用 ProfileView.jl 或类似软件包对配置文件进行可视化浏览代码.

Also, you can use ProfileView.jl or similar packages for visual exploration of the profiled code.

并且不是完全剖析,但是在实践软件包中非常有用的是 TimerOutputs.jl

UPD:由于Julia是一种编译语言,因此测量单个行的时序没有任何意义,因为执行的实际代码可能与Julia编写的代码完全不同.

UPD: Since Julia is a compiling language it makes no sense to measure timing of individual lines, since the actual code that is executed can be very different from what is written in Julia.

例如遵循朱莉娅代码

function f()
    x = 0
    for i in 0:100_000_000
        x += i
    end

    x
end

降低到

julia> @code_llvm f()
;  @ REPL[8]:1 within `f'
define i64 @julia_f_594() {
top:
;  @ REPL[8]:7 within `f'
  ret i64 5000000050000000
}

即根本没有循环.这就是为什么使用行出现在所有回溯集合中的频率代替执行时间代理指标的原因.当然,它与执行时间并不相同,但是它可以很好地估计瓶颈所在的位置,因为执行时间长的行在回溯中的出现频率更高.

I.e. there is no loop at all. This is why instead of execution time proxy metric of how often a line appears in the set of all backtraces is used. Of course, it is not the same as the execution time, but it gives a good approximation of where the bottleneck is because lines with long execution time appear in backtraces more often.

这篇关于可以逐行分析Julia代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆