我可以通过什么方式对 Julia 函数进行基准测试?

Pse*_*erd 12 julia flux.jl

背景

我自学了机器学习,最近开始深入研究 Julia 机器学习生态系统。


来自 python 背景并且有一些 Tensorflow 和OpenCV /skimage经验,我想将 Julia ML 库(Flux/JuliaImages)与它的同行进行基准测试,看看它真正执行CV(任何)任务的速度有多快或多慢, 并决定是否我应该转而使用 Julia。

我知道如何使用这样的timeit模块来获取在 python 中执行函数所花费的时间:

#Loading an Image using OpenCV

s = """\
img = cv2.imread('sample_image.png', 1)
"""
setup = """\
import timeit
"""
print(str(round((timeit.timeit(stmt = s, setup = setup, number = 1))*1000, 2)) + " ms")
#printing the time taken in ms rounded to 2 digits
Run Code Online (Sandbox Code Playgroud)

如何使用适当的库(在本例中为JuliaImages)比较在 Julia 中执行相同任务的函数的执行时间。

Julia 是否为 time/benchmark 提供任何函数/宏?

Jef*_*off 10

using BenchmarkTools是对 Julia 函数进行基准测试的推荐方法。除非您正在计时需要很长时间的东西,否则请使用从它导出@benchmark的不那么冗长的@btime宏。因为这些宏背后的机制会多次评估目标函数,@time所以对于对运行缓慢的事物进行基准测试非常有用(例如,涉及磁盘访问或非常耗时的计算)。

正确使用@btime@benchmark正确使用很重要,这样可以避免误导结果。通常,您正在对带有一个或多个参数的函数进行基准测试。基准测试时,所有参数都应该是外部变量:(没有基准宏)

x = 1
f(x)
# do not use f(1)
Run Code Online (Sandbox Code Playgroud)

该函数将被评估多次。为了防止函数参数在计算函数时被重新计算,我们必须通过在$用作参数的每个变量的名称前加上前缀 a来标记每个参数。基准测试宏使用它来指示变量应该在基准测试过程开始时被评估(解析)一次,然后结果将直接重用:

julia> using BenchmarkTools
julia> a = 1/2;
julia> b = 1/4;
julia> c = 1/8;
julia> a, b, c
(0.5, 0.25, 0.125)

julia> function sum_cosines(x, y, z)
         return cos(x) + cos(y) + cos(z)
       end;

julia> @btime sum_cosines($a, $b, $c);  # the `;` suppresses printing the returned value
  11.899 ns (0 allocations: 0 bytes)    # calling the function takes ~12 ns (nanoseconds)
                                        # the function does not allocate any memory
Run Code Online (Sandbox Code Playgroud)
# if we omit the '$', what we see is misleading
julia> @btime sum_cosines(a, b, c);    # the function appears more than twice slower 
 28.441 ns (1 allocation: 16 bytes)    # the function appears to be allocating memory
Run Code Online (Sandbox Code Playgroud)
# @benchmark can be used the same way that @btime is used
julia> @benchmark sum_cosines($a,$b,$c) # do not use a ';' here
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     12.111 ns (0.00% GC)
  median time:      12.213 ns (0.00% GC)
  mean time:        12.500 ns (0.00% GC)
  maximum time:     39.741 ns (0.00% GC)
  --------------
  samples:          1500
  evals/sample:     999
Run Code Online (Sandbox Code Playgroud)

虽然存在可以调整的参数,但默认值通常效果很好。有关适用于有经验的用户的 BenchmarkTools 的更多信息,请参阅手册


Pse*_*erd 7

Julia为计时/基准代码运行时提供了两个。这些是 :

  • @时间
  • @benchmark:外部,安装Pkg.add("BenchmarkTools")

使用 BenchmarkTools 的 @benchmark 非常简单,有助于您比较两种语言的速度。@benchark针对您提供的 python 工作台使用的示例。

using Images, FileIO, BenchmarkTools

@benchmark img = load("sample_image.png")
Run Code Online (Sandbox Code Playgroud)

输出 :

BenchmarkTools.Trial: 
  memory estimate:  3.39 MiB
  allocs estimate:  322
  --------------
  minimum time:     76.631 ms (0.00% GC)
  median time:      105.579 ms (0.00% GC)
  mean time:        110.319 ms (0.41% GC)
  maximum time:     209.470 ms (0.00% GC)
  --------------
  samples:          46
  evals/sample:     1
Run Code Online (Sandbox Code Playgroud)

现在要比较平均时间,您应该将samples(46) 作为 Python timeit 代码中的数字,并将其除以相同的数字以获得平均执行时间。

using Images, FileIO, BenchmarkTools

@benchmark img = load("sample_image.png")
Run Code Online (Sandbox Code Playgroud)

您可以按照此过程对 Julia 和 Python 中的任何函数进行基准测试。希望你的疑惑已经解开。


注意从统计的角度来看,@benchmark 比@time 好很多。

  • 请注意,时序噪声大多是正的,这意味着最短时间通常(并不总是)提供更多信息。`@btime` 和 `@belapsed` 只返回最短时间。 (2认同)