【问题标题】:Measuring time in Julia and the behavior of function calling在 Julia 中测量时间和函数调用的行为
【发布时间】:2021-09-23 09:44:15
【问题描述】:

我有两个问题,我会写在这篇文章的底部。

背景

我使用TimerOutputs.jl 包测量了时间,我发现了难以理解的行为。

我编写了名为f的函数,并测量了以下4个部分:whole in,i section,x section,whole out

代码和结果如下:

[输入1]

using TimerOutputs
const to = TimerOutput()

f = function()
    @timeit to "whole in" begin
        a = [];
    
        @timeit to "i section" begin
            for i in 1:1000
                push!(a,randn(5,5))
            end
        end
    
        @timeit to "x section" begin
            for x in 1:1000
                push!(a,randn(5,5) .+ x )
            end
        end
    end
end

@timeit to "whole out" f()

show(to)

[输出1]

 ────────────────────────────────────────────────────────────────────────
                                 Time                   Allocations      
                         ──────────────────────   ───────────────────────
    Tot / % measured:         106ms / 95.8%           12.0MiB / 99.4%    

 Section         ncalls     time   %tot     avg     alloc   %tot      avg
 ────────────────────────────────────────────────────────────────────────
 whole out            1    102ms   100%   102ms   11.9MiB  100%   11.9MiB
   whole in           1    310μs  0.30%   310μs    878KiB  7.18%   878KiB
     x section        1    155μs  0.15%   155μs    579KiB  4.73%   579KiB
     i section        1    151μs  0.15%   151μs    298KiB  2.43%   298KiB
 ────────────────────────────────────────────────────────────────────────

[输入 2]

TimerOutputs.time(to["whole out"])/TimerOutputs.time(to["whole out"]["whole in"])

[输出2]

328.0928134063809

问题

第一季度:

为什么运行whole out的时间比运行whole in的时间长328倍?

我猜这种差异来自调用函数时 CPU 和内存的行为机制,但我没有任何知识。我要说清楚。

第二季度:

我想测量运行f 的时间并在f 中使用结果(whole out@timeit 的输出)。我该怎么做?

任何信息将不胜感激。

【问题讨论】:

  • 一些 cmets:(1) 为了避免测量预编译时间,正如答案中提到的可能是你所看到的,你可能宁愿使用 BenchmarkTools.jl 中的 @benchmark 之类的东西。 (2)如果你关心性能的程度,你关心细节的时间,你可能不应该使用push!,如果可能的话。而是预先分配并就地填充(或者至少给 push! 一个 sizehint! 使用)。

标签: time timer jupyter-notebook julia


【解决方案1】:

Q1:您可能正在计时f 的预编译。尝试在之前运行一次f 或重置计时器:

julia> reset_timer!(to) ;

julia> @timeit to "whole out" f()

julia> show(to)
 ────────────────────────────────────────────────────────────────────────
                                 Time                   Allocations
                         ──────────────────────   ───────────────────────
    Tot / % measured:         4.02s / 0.02%           0.99MiB / 86.4%

 Section         ncalls     time   %tot     avg     alloc   %tot      avg
 ────────────────────────────────────────────────────────────────────────
 whole out            1    856μs   100%   856μs    879KiB  100%    879KiB
   whole in           1    842μs  98.4%   842μs    878KiB  100%    878KiB
     x section        1    515μs  60.2%   515μs    579KiB  65.8%   579KiB
     i section        1    321μs  37.6%   321μs    298KiB  33.9%   298KiB
 ────────────────────────────────────────────────────────────────────────

Q2:我不确定你的意思。也许最好在一个单独的问题中再次提出这个问题,并附上您迄今为止尝试过的相应的可复制粘贴代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 2023-03-09
    • 2011-09-09
    相关资源
    最近更新 更多