时间取决于对象的大小。
考虑以下对整数进行乘法、加法和比较的函数:
min200(T) = minimum(x*x+x for x in UnitRange{T}(1:200))
这里是时间using BenchmarkTools:
julia> @btime min200(Int16);
9.409 ns (0 allocations: 0 bytes)
julia> @btime min200(Int32);
14.329 ns (0 allocations: 0 bytes)
julia> @btime min200(Int64);
47.267 ns (0 allocations: 0 bytes)
julia> @btime min200(Int128);
256.160 ns (0 allocations: 0 bytes)
请注意,这种差异归结为汇编代码。让我们看看如何编译两个整数的加法。
julia> @code_native +(Int32(5), Int32(7))
.text
; ┌ @ int.jl:87 within `+`
pushq %rbp
movq %rsp, %rbp
leal (%rcx,%rdx), %eax
popq %rbp
retq
nopl (%rax)
; └
julia> @code_native +(5,7)
.text
; ┌ @ int.jl:87 within `+`
pushq %rbp
movq %rsp, %rbp
leaq (%rcx,%rdx), %rax
popq %rbp
retq
nopw (%rax,%rax)
; └
在第二个代码中可以看到 32 位添加使用了 32 位注册表 %eax vs 64 位 %rax。
此外,当您使用 @simd 或通过 CuArrays 进行 GPU 计算等功能时,这些差异可能会变得更加显着。