time - Julia :@time 一组函数

标签 time julia

我有一个 script.jl看起来或多或少是这样的:

...

function main()
    a::... = function1()
    b::... = function2(...)
    c::... = function3(...)
    A::... = function4(...)
    B::... = function5(...)
    C::... = function6(...)
end

main()

我不能@time main()因为函数 1-3 是输入函数,因此它们的执行时间取决于用户的速度有多快或多慢。
有没有办法只计时功能 4-6?
我不知道,像这样:
...

function main()
    a::... = function1()
    b::... = function2(...)
    c::... = function3(...)
    @time(
    A::... = function4(...)
    B::... = function5(...)
    C::... = function6(...)
    )
end

main()

最佳答案

注意: 我想这只是一个例子,但语法 C::... 不是有效的 Julia 语法,如果你提供简单但功能性的例子会更好。

如果您想要独立的计时,您可以使用 @time 宏注释在您对计时感兴趣的每个表达式之前:

function main()
    a = function1()
    b = function2(...)
    c = function3(...)
    @time A = function4(...)
    @time B = function5(...)
    @time C = function6(...)
end

main()

或者:

function main()
    a = function1()
    b = function2(...)
    c = function3(...)
    @time begin
        A = function4(...)
        B = function5(...)
        C = function6(...)
    end
end

main()

这类似于@Gomiero 的答案,只是为了使用 @time 宏为多个函数计时,您需要引入一个新块并将所有内容放入其中。

还要检查尚未注册的包 Benchmarks ,即:

julia> Pkg.add("https://github.com/johnmyleswhite/Benchmarks.jl.git")

julia> using Benchmarks

julia> function test()
           x = 0
           for i in 1:1000_000_000
               x += 1
           end
           return x
       end
test (generic function with 1 method)

julia> @time test()    # JIT warmup!
  0.003669 seconds (1.71 k allocations: 90.799 KB)
1000000000

julia> @time test()
  0.000002 seconds (5 allocations: 176 bytes)
1000000000

julia> @benchmark test()
================ Benchmark Results ========================
     Time per evaluation: 6.03 ns [5.92 ns, 6.13 ns]
Proportion of time in GC: 0.00% [0.00%, 0.00%]
        Memory allocated: 0.00 bytes
   Number of allocations: 0 allocations
       Number of samples: 6301
   Number of evaluations: 811601
         R² of OLS model: 0.951
 Time spent benchmarking: 2.96 s

如果要对多个表达式计时,请使用 begin 块方法。

关于time - Julia :@time 一组函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35683052/

相关文章:

mysql - 将日期和时间列合并到 MySQL 中的 DATETIME 列

php - 将时间转换为 MySQL 时间

julia - 让 Julia 在 LOAD_PATH 中查找文件

julia - 如何在 Julia 中制作 Int8 文字?

parallel-processing - 在多台机器上运行 julia 代码

julia - 我可以在 Julia 中为类型字段取别名吗?

ruby - Ruby 中的 Time.now 与 Time.new

ios - 如何在 React Native 中获取当前日期?

php - mySQL - PHP 日期 = 时间加 12 小时

julia - 如何在 Julia 中测试数组中的所有元素是否具有相同的值