资讯专栏INFORMATION COLUMN

让代码飞起来——高性能Julia学习笔记(二)

noONE / 951人阅读

摘要:首发于接上一篇让代码飞起来高性能学习笔记一,继续整理高性能学习笔记。和都只能表示特定的整数范围,超过范围会。通用代码一般会用,这就有可能导致性能问题。

首发于 https://magicly.me/hpc-julia-2/

接上一篇:让代码飞起来——高性能 Julia 学习笔记(一), 继续整理高性能 Julia 学习笔记。

数字

Julia 中 Number 的 size 就跟 C 语言里面一样, 直接依赖于底层的 CPU/OS, 32 位 OS 上 integer 默认是 32 位, 64 位 OS 上 integer 默认是 64 位。

可以用bitstring查看 number 的二进制表示:

</>复制代码

  1. julia > bitstring(3)
  2. "0000000000000000000000000000000000000000000000000000000000000011"
  3. julia > bitstring(-3)
  4. "1111111111111111111111111111111111111111111111111111111111111101"

有时候数字可能会被 boxed,Julia 的 compiler 会自动 boxing/unboxing。

Int64 和 Int32 都只能表示特定的整数范围, 超过范围会 overflow。

</>复制代码

  1. julia> typemax(Int64)
  2. 9223372036854775807
  3. julia> bitstring(typemax(Int64))
  4. "0111111111111111111111111111111111111111111111111111111111111111"
  5. julia> typemin(Int64)
  6. -9223372036854775808
  7. julia> bitstring(typemin(Int64))
  8. "1000000000000000000000000000000000000000000000000000000000000000"
  9. julia> typemax(Int64) + 1
  10. -9223372036854775808
  11. julia> typemin(Int64) - 1
  12. 9223372036854775807

如果要表示任意精度的话, 可以用BitInt

</>复制代码

  1. julia> big(typemax(Int64)) + 1
  2. 9223372036854775808

float point 遵循IEEE 754标准。

</>复制代码

  1. julia> bitstring(1.5)
  2. "0011111111111000000000000000000000000000000000000000000000000000"
  3. julia> bitstring(-1.5)
  4. "1011111111111000000000000000000000000000000000000000000000000000"

无符号整数 Unsigned integer 可以用 UInt64 和 UInt32 表示, 不同类型可以转, 但是如果超出可表示范围会报错。

</>复制代码

  1. julia> UInt64(UInt32(1))
  2. 0x0000000000000001
  3. julia> UInt32(UInt64(1))
  4. 0x00000001
  5. julia> UInt32(typemax(UInt64))
  6. ERROR: InexactError: trunc(UInt32, 18446744073709551615)
  7. Stacktrace:
  8. [1] throw_inexacterror(::Symbol, ::Any, ::UInt64) at ./boot.jl:567
  9. [2] checked_trunc_uint at ./boot.jl:597 [inlined]
  10. [3] toUInt32 at ./boot.jl:686 [inlined]
  11. [4] UInt32(::UInt64) at ./boot.jl:721
  12. [5] top-level scope at none:0

有时候不需要考虑是否溢出, 可以直接用%符号取低位, 速度还更快:

</>复制代码

  1. julia> (typemax(UInt64) - 1) % UInt32
  2. 0xfffffffe

精度和效率平衡
有时候为了更高的效率, 可以使用@fastmath宏, 它会放宽一些限制, 包括检查 NaN 或 Infinity 等, 类似于 GCC 中的-ffast-math编译选项。

</>复制代码

  1. julia> function sum_diff(x)
  2. n = length(x); d = 1/(n-1)
  3. s = zero(eltype(x))
  4. s = s + (x[2] - x[1]) / d
  5. for i = 2:length(x)-1
  6. s = s + (x[i+1] - x[i+1]) / (2*d)
  7. end
  8. s = s + (x[n] - x[n-1])/d
  9. end
  10. sum_diff (generic function with 1 method)
  11. julia> function sum_diff_fast(x)
  12. n=length(x); d = 1/(n-1)
  13. s = zero(eltype(x))
  14. @fastmath s = s + (x[2] - x[1]) / d
  15. @fastmath for i = 2:n-1
  16. s = s + (x[i+1] - x[i+1]) / (2*d)
  17. end
  18. @fastmath s = s + (x[n] - x[n-1])/d
  19. end
  20. sum_diff_fast (generic function with 1 method)
  21. julia> t=rand(2000);
  22. julia> sum_diff(t)
  23. 1124.071808538789
  24. julia> sum_diff_fast(t)
  25. 1124.0718085387887
  26. julia> using BenchmarkTools
  27. julia> @benchmark sum_diff(t)
  28. BenchmarkTools.Trial:
  29. memory estimate: 16 bytes
  30. allocs estimate: 1
  31. --------------
  32. minimum time: 4.447 μs (0.00% GC)
  33. median time: 4.504 μs (0.00% GC)
  34. mean time: 4.823 μs (0.00% GC)
  35. maximum time: 17.318 μs (0.00% GC)
  36. --------------
  37. samples: 10000
  38. evals/sample: 7
  39. julia> @benchmark sum_diff_fast(t)
  40. BenchmarkTools.Trial:
  41. memory estimate: 16 bytes
  42. allocs estimate: 1
  43. --------------
  44. minimum time: 574.951 ns (0.00% GC)
  45. median time: 580.831 ns (0.00% GC)
  46. mean time: 621.044 ns (1.04% GC)
  47. maximum time: 65.017 μs (99.06% GC)
  48. --------------
  49. samples: 10000
  50. evals/sample: 183

差异还是蛮大的, 差不多 8 倍差距! 我们可以用macroexpand看看宏展开的代码:

</>复制代码

  1. julia> ex = :(@fastmath for i in 2:n-1
  2. s = s + (x[i+1] - x[i+1]) / (2*d)
  3. end)
  4. :(#= REPL[57]:1 =# @fastmath for i = 2:n - 1
  5. #= REPL[57]:2 =#
  6. s = s + (x[i + 1] - x[i + 1]) / (2d)
  7. end)
  8. julia> macroexpand(Main, ex)
  9. :(for i = 2:(Base.FastMath).sub_fast(n, 1)
  10. #= REPL[57]:2 =#
  11. s = (Base.FastMath).add_fast(s, (Base.FastMath).div_fast((Base.FastMath).sub_fast(x[(Base.FastMath).add_fast(i, 1)], x[(Base.FastMath).add_fast(i, 1)]), (Base.FastMath).mul_fast(2, d)))
  12. end)

可以看到, 主要是把普通的加减乘除换成了Base.FastMath中的方法。

本章最后介绍了K-B-N求和减少误差,以及 Subnormal numbers, 感觉都是科学计算里面才会用到的, 暂时没管。

数组

科学计算以及人工智能里面有大量向量、矩阵运算, 在 Julia 里都直接对应到 Array。 Vector 和 Matrix 其实就是 Array 的特例:

</>复制代码

  1. julia> Vector
  2. Array{T,1} where T
  3. julia> Matrix
  4. Array{T,2} where T

即 Vector 是一维数组, Matrix 是二维数组。从这里也可以看出, Julia 中类型的参数类型可以是具体的 value, 比如这里的 1 和 2。

Julia 中 Array 数据是连续存放的, 如下图:

跟存放指针相比好处是少了一次内存访问, 并且可以更好地利用 CPU 的 pipeline 和 cache,以及 SIMD。

Julia 中多维数组是按列优先存储的(类似 Matlab 和 Fortran),这点跟 C 语言中不一样:

按照数据在内存中的布局来读取数据, 能显著提高效率。

</>复制代码

  1. julia> function col_iter(x)
  2. s=zero(eltype(x))
  3. for i = 1:size(x, 2)
  4. for j = 1:size(x, 1)
  5. s = s + x[j, i] ^ 2
  6. x[j, i] = s
  7. end
  8. end
  9. end
  10. col_iter (generic function with 1 method)
  11. julia> function row_iter(x)
  12. s=zero(eltype(x))
  13. for i = 1:size(x, 1)
  14. for j = 1:size(x, 2)
  15. s = s + x[i, j] ^ 2
  16. x[i, j] = s
  17. end
  18. end
  19. end
  20. row_iter (generic function with 1 method)
  21. julia> a = rand(1000, 1000);
  22. julia> @benchmark row_iter(a)
  23. BenchmarkTools.Trial:
  24. memory estimate: 0 bytes
  25. allocs estimate: 0
  26. --------------
  27. minimum time: 2.217 ms (0.00% GC)
  28. median time: 2.426 ms (0.00% GC)
  29. mean time: 2.524 ms (0.00% GC)
  30. maximum time: 11.723 ms (0.00% GC)
  31. --------------
  32. samples: 1974
  33. evals/sample: 1
  34. julia> @benchmark col_iter(a)
  35. BenchmarkTools.Trial:
  36. memory estimate: 0 bytes
  37. allocs estimate: 0
  38. --------------
  39. minimum time: 815.984 μs (0.00% GC)
  40. median time: 910.121 μs (0.00% GC)
  41. mean time: 917.850 μs (0.00% GC)
  42. maximum time: 1.292 ms (0.00% GC)
  43. --------------
  44. samples: 5416
  45. evals/sample: 1

Julia runtime 会对 array 访问做 bound 判断, 看是否超出边界。 超出边界的访问会导致很多 bugs,甚至是安全问题。 另一方面, bound check 会带来额外的开销,如果你能很确定不会越界, 那可以用@inbound 宏告诉 Julia 不要做 bound check, 效率会提高不少。

</>复制代码

  1. julia> function prefix_bounds(a, b)
  2. for i = 2:size(a, 1)
  3. a[i] = b[i-1] + b[i]
  4. end
  5. end
  6. prefix_bounds (generic function with 1 method)
  7. julia> function prefix_inbounds(a, b)
  8. @inbounds for i = 2:size(a, 1)
  9. a[i] = b[i-1] + b[i]
  10. end
  11. end
  12. prefix_inbounds (generic function with 1 method)
  13. julia> a = rand(1000);
  14. julia> b = rand(1000);
  15. julia> @benchmark prefix_bounds(a, b)
  16. BenchmarkTools.Trial:
  17. memory estimate: 0 bytes
  18. allocs estimate: 0
  19. --------------
  20. minimum time: 742.360 ns (0.00% GC)
  21. median time: 763.264 ns (0.00% GC)
  22. mean time: 807.497 ns (0.00% GC)
  23. maximum time: 1.968 μs (0.00% GC)
  24. --------------
  25. samples: 10000
  26. evals/sample: 125
  27. julia> @benchmark prefix_inbounds(a, b)
  28. BenchmarkTools.Trial:
  29. memory estimate: 0 bytes
  30. allocs estimate: 0
  31. --------------
  32. minimum time: 179.294 ns (0.00% GC)
  33. median time: 181.826 ns (0.00% GC)
  34. mean time: 185.124 ns (0.00% GC)
  35. maximum time: 635.909 ns (0.00% GC)
  36. --------------
  37. samples: 10000
  38. evals/sample: 690

慎用@inbound!!! 最好是限制 loop 直接依赖于 array 长度, 比如:for i in 1:length(array)这种形式。

可以在启动的时候加上--check-bounds=yes/no来全部开启或者关闭(优先级高于@inbound 宏)bound check! 再说一次, 慎用!

Julia 内置了很多操作 Array 的函数, 一般都提供两个版本, 注意可变和不可变版本差异很大!!!

</>复制代码

  1. julia> a = rand(1000);
  2. julia> @benchmark sort(a)
  3. BenchmarkTools.Trial:
  4. memory estimate: 7.94 KiB
  5. allocs estimate: 1
  6. --------------
  7. minimum time: 16.050 μs (0.00% GC)
  8. median time: 17.493 μs (0.00% GC)
  9. mean time: 18.933 μs (0.00% GC)
  10. maximum time: 726.416 μs (0.00% GC)
  11. --------------
  12. samples: 10000
  13. evals/sample: 1
  14. julia> @benchmark sort!(a)
  15. BenchmarkTools.Trial:
  16. memory estimate: 0 bytes
  17. allocs estimate: 0
  18. --------------
  19. minimum time: 4.868 μs (0.00% GC)
  20. median time: 4.997 μs (0.00% GC)
  21. mean time: 5.282 μs (0.00% GC)
  22. maximum time: 13.772 μs (0.00% GC)
  23. --------------
  24. samples: 10000
  25. evals/sample: 7

我们可以看到, 不可变版本sort无论时间还是内存占用和分配上都比可变版本sort!高。 这很容易理解, 不可变版本需要 clone 一份数据出来,而不是直接修改参数。 根据需要选择最适合的版本。

类似的,合理利用预分配,可以有效降低时间和内存占用。

</>复制代码

  1. julia> function xpow(x)
  2. return [x x^2 x^3 x^4]
  3. end
  4. xpow (generic function with 1 method)
  5. julia> function xpow_loop(n) s= 0
  6. for i = 1:n
  7. s = s + xpow(i)[2]
  8. end
  9. s
  10. end
  11. xpow_loop (generic function with 1 method)
  12. julia> @benchmark xpow_loop(1_000_000)
  13. BenchmarkTools.Trial:
  14. memory estimate: 167.84 MiB
  15. allocs estimate: 4999441
  16. --------------
  17. minimum time: 68.342 ms (4.11% GC)
  18. median time: 70.378 ms (5.21% GC)
  19. mean time: 71.581 ms (6.04% GC)
  20. maximum time: 120.430 ms (39.92% GC)
  21. --------------
  22. samples: 70
  23. evals/sample: 1
  24. julia> function xpow!(result::Array{Int, 1}, x)
  25. @assert length(result) == 4
  26. result[1] = x
  27. result[2] = x^2
  28. result[3] = x^3
  29. result[4] = x^4
  30. end
  31. xpow! (generic function with 1 method)
  32. julia> function xpow_loop_noalloc(n)
  33. r = [0, 0, 0, 0]
  34. s= 0
  35. for i = 1:n
  36. xpow!(r, i)
  37. s = s + r[2]
  38. end
  39. s
  40. end
  41. xpow_loop_noalloc (generic function with 1 method)
  42. julia> @benchmark xpow_loop_noalloc(1_000_000)
  43. BenchmarkTools.Trial:
  44. memory estimate: 112 bytes
  45. allocs estimate: 1
  46. --------------
  47. minimum time: 7.314 ms (0.00% GC)
  48. median time: 7.486 ms (0.00% GC)
  49. mean time: 7.599 ms (0.00% GC)
  50. maximum time: 9.580 ms (0.00% GC)
  51. --------------
  52. samples: 658
  53. evals/sample: 1

Julia 中做 Array slicing 很容易,类似 python 的语法:

</>复制代码

  1. julia> a = collect(1:100);
  2. julia> a[1:10]
  3. 10-element Array{Int64,1}:
  4. 1
  5. 2
  6. 3
  7. 4
  8. 5
  9. 6
  10. 7
  11. 8
  12. 9
  13. 10

语法容易使用很容易造成滥用, 导致性能问题, 因为:array slicing 会 copy 一个副本! 我们来计算一个矩阵的每列的和, 简单实现如下:

</>复制代码

  1. julia> function sum_vector(x::Array{Float64, 1})
  2. s = 0.0
  3. for i = 1:length(x)
  4. s = s + x[i]
  5. end
  6. return s
  7. end
  8. sum_vector (generic function with 1 method)
  9. julia> function sum_cols_matrix(x::Array{Float64, 2})
  10. num_cols = size(x, 2)
  11. s = zeros(num_cols)
  12. for i = 1:num_cols
  13. s[i] = sum_vector(x[:, i])
  14. end
  15. return s
  16. end
  17. sum_cols_matrix (generic function with 1 method)
  18. julia> t = rand(1000, 1000);
  19. julia> @benchmark sum_cols_matrix(t)
  20. BenchmarkTools.Trial:
  21. memory estimate: 7.76 MiB
  22. allocs estimate: 1001
  23. --------------
  24. minimum time: 1.703 ms (0.00% GC)
  25. median time: 2.600 ms (0.00% GC)
  26. mean time: 2.902 ms (19.10% GC)
  27. maximum time: 40.978 ms (94.27% GC)
  28. --------------
  29. samples: 1719
  30. evals/sample: 1

x[:, j]是取第 j 列的所有元素。 算法很简单, 定义一个函数sum_vector计算向量的和, 然后在sum_cols_matrix中取每一列传过去。

由于x[:, j]这样 slicing 会 copy 元素, 所以内存占用和分配都比较大。 Julia 提供了view函数,可以复用父数组的元素而不是 copy, 具体用法可以参考https://docs.julialang.org/en... 。

由于view返回的是SubArray类型, 所以我们先修改一下sum_vector的参数类型为AbstractArray

</>复制代码

  1. julia> function sum_vector2(x::AbstractArray)
  2. s = 0.0
  3. for i = 1:length(x)
  4. s = s + x[i]
  5. end
  6. return s
  7. end
  8. sum_vector2 (generic function with 1 method)
  9. julia> function sum_cols_matrix_views(x::Array{Float64, 2})
  10. num_cols = size(x, 2)
  11. s = zeros(num_cols)
  12. for i = 1:num_cols
  13. s[i] = sum_vector2(view(x, :, i))
  14. end
  15. return s
  16. end
  17. sum_cols_matrix_views (generic function with 1 method)
  18. julia>
  19. julia> @benchmark sum_cols_matrix_views(t)
  20. BenchmarkTools.Trial:
  21. memory estimate: 7.94 KiB
  22. allocs estimate: 1
  23. --------------
  24. minimum time: 812.209 μs (0.00% GC)
  25. median time: 883.884 μs (0.00% GC)
  26. mean time: 897.888 μs (0.00% GC)
  27. maximum time: 6.552 ms (0.00% GC)
  28. --------------
  29. samples: 5474
  30. evals/sample: 1

可以看到,提升还是蛮大的。

SIMD全称Single Instruction Multiple Data, 是现代 CPU 的特性, 可以一条指令操作多条数据。 来加两个向量试试:

</>复制代码

  1. julia> x = zeros(1_000_000); y = rand(1_000_000); z = rand(1_000_000);
  2. julia> function sum_vectors!(x, y, z)
  3. n = length(x)
  4. for i = 1:n
  5. x[i] = y[i] + z[i]
  6. end
  7. end
  8. sum_vectors! (generic function with 1 method)
  9. julia> function sum_vectors_inbounds!(x, y, z)
  10. n = length(x)
  11. @inbounds for i = 1:n
  12. x[i] = y[i] + z[i]
  13. end
  14. end
  15. sum_vectors_inbounds! (generic function with 1 method)
  16. julia> function sum_vectors_inbounds_simd!(x, y, z)
  17. n = length(x)
  18. @inbounds @simd for i = 1:n
  19. x[i] = y[i] + z[i]
  20. end
  21. end
  22. sum_vectors_inbounds_simd! (generic function with 1 method)
  23. julia> @benchmark sum_vectors!(x, y, z)
  24. BenchmarkTools.Trial:
  25. memory estimate: 0 bytes
  26. allocs estimate: 0
  27. --------------
  28. minimum time: 1.117 ms (0.00% GC)
  29. median time: 1.156 ms (0.00% GC)
  30. mean time: 1.174 ms (0.00% GC)
  31. maximum time: 1.977 ms (0.00% GC)
  32. --------------
  33. samples: 4234
  34. evals/sample: 1
  35. julia> @benchmark sum_vectors_inbounds!(x, y, z)
  36. BenchmarkTools.Trial:
  37. memory estimate: 0 bytes
  38. allocs estimate: 0
  39. --------------
  40. minimum time: 1.118 ms (0.00% GC)
  41. median time: 1.148 ms (0.00% GC)
  42. mean time: 1.158 ms (0.00% GC)
  43. maximum time: 1.960 ms (0.00% GC)
  44. --------------
  45. samples: 4294
  46. evals/sample: 1
  47. julia> @benchmark sum_vectors_inbounds_simd!(x, y, z)
  48. BenchmarkTools.Trial:
  49. memory estimate: 0 bytes
  50. allocs estimate: 0
  51. --------------
  52. minimum time: 1.118 ms (0.00% GC)
  53. median time: 1.145 ms (0.00% GC)
  54. mean time: 1.155 ms (0.00% GC)
  55. maximum time: 2.080 ms (0.00% GC)
  56. --------------
  57. samples: 4305
  58. evals/sample: 1

这个测试结果, 无论用@inbounds还是@simd,都没有提速(难道 Julia 现在默认会使用 SIMD?), 测试了几次都不行。 另外, 我在测试https://github.com/JuliaCompu... 的时候, 也发现有没有 simd 差别不大, 如有知道原因的童鞋欢迎留言告知, 非常谢谢。

另外说Yeppp这个包也能提高速度, 还没有测试。

如果我们设计的函数给别人用, 那么 API 会设计的比较通用(比如参数设计成 AbstractArray), 可能会接受各种类型的 Array,这时候需要小心如何遍历数组

有两种 indexing 方式。 一种是 linear indexing, 比如是一个三维 array, 每维度 10 个元素, 则可以用 x[1], x[2], ..., x[1000]连续地访问数组。 第二种叫 cartesian indexing, 访问方式为 x[i, j, k]。 某些数组不是连续的(比如 view 生成的 SubArray),这时候用 cartesian indexing 访问会比用 linear indexing 访问快, 因为 linear indexing 需要除法转化成每一维的下标。 通用代码一般会用 linear indexing, 这就有可能导致性能问题。

</>复制代码

  1. julia> function mysum_linear(a::AbstractArray)
  2. s=zero(eltype(a))
  3. for i = 1:length(a)
  4. s=s+a[i]
  5. end
  6. return s
  7. end
  8. mysum_linear (generic function with 1 method)
  9. julia> mysum_linear(1:1000000)
  10. 500000500000
  11. julia> mysum_linear(reshape(1:1000000, 100, 100, 100))
  12. 500000500000
  13. julia> mysum_linear(reshape(1:1000000, 1000, 1000))
  14. 500000500000
  15. julia> mysum_linear(view(reshape(1:1000000, 1000, 1000), 1:500, 1:500) )
  16. 62437625000
  17. julia> @benchmark mysum_linear(1:1000000)
  18. BenchmarkTools.Trial:
  19. memory estimate: 0 bytes
  20. allocs estimate: 0
  21. --------------
  22. minimum time: 1.380 ns (0.00% GC)
  23. median time: 1.426 ns (0.00% GC)
  24. mean time: 1.537 ns (0.00% GC)
  25. maximum time: 13.475 ns (0.00% GC)
  26. --------------
  27. samples: 10000
  28. evals/sample: 1000
  29. julia> @benchmark mysum_linear(reshape(1:1000000, 1000, 1000))
  30. BenchmarkTools.Trial:
  31. memory estimate: 0 bytes
  32. allocs estimate: 0
  33. --------------
  34. minimum time: 1.379 ns (0.00% GC)
  35. median time: 1.392 ns (0.00% GC)
  36. mean time: 1.482 ns (0.00% GC)
  37. maximum time: 23.089 ns (0.00% GC)
  38. --------------
  39. samples: 10000
  40. evals/sample: 1000
  41. julia> @benchmark mysum_linear(view(reshape(1:1000000, 1000, 1000), 1:500, 1:500))
  42. BenchmarkTools.Trial:
  43. memory estimate: 0 bytes
  44. allocs estimate: 0
  45. --------------
  46. minimum time: 1.016 ms (0.00% GC)
  47. median time: 1.047 ms (0.00% GC)
  48. mean time: 1.071 ms (0.00% GC)
  49. maximum time: 2.211 ms (0.00% GC)
  50. --------------
  51. samples: 4659
  52. evals/sample: 1

可以看到最后一次测试, 元素总数更少了, 但是时间更长, 原因就是数组不是连续的, 但是又用了 linear indexing。 最简单的解决方法是直接迭代元素, 而不是迭代下标。

</>复制代码

  1. julia> function mysum_in(a::AbstractArray)
  2. s = zero(eltype(a))
  3. for i in a
  4. s=s+ i
  5. end
  6. end
  7. mysum_in (generic function with 1 method)
  8. julia> @benchmark mysum_in(view(reshape(1:1000000, 1000, 1000), 1:500, 1:500))
  9. BenchmarkTools.Trial:
  10. memory estimate: 0 bytes
  11. allocs estimate: 0
  12. --------------
  13. minimum time: 224.538 μs (0.00% GC)
  14. median time: 238.493 μs (0.00% GC)
  15. mean time: 246.847 μs (0.00% GC)
  16. maximum time: 413.477 μs (0.00% GC)
  17. --------------
  18. samples: 10000
  19. evals/sample: 1

可以看到, 跟 linear indexing 相比, 效率是 4 倍多。 但是如果我们需要下标怎么办呢?可以用eachindex()方法,每一种 array 都会定义一个优化过的eachindex()方法:

</>复制代码

  1. julia> function mysum_eachindex(a::AbstractArray)
  2. s = zero(eltype(a))
  3. for i in eachindex(a)
  4. s = s + a[i]
  5. end
  6. end
  7. mysum_eachindex (generic function with 1 method)
  8. julia> @benchmark mysum_eachindex(view(reshape(1:1000000, 1000, 1000), 1:500, 1:500))
  9. BenchmarkTools.Trial:
  10. memory estimate: 0 bytes
  11. allocs estimate: 0
  12. --------------
  13. minimum time: 243.295 μs (0.00% GC)
  14. median time: 273.168 μs (0.00% GC)
  15. mean time: 285.002 μs (0.00% GC)
  16. maximum time: 4.191 ms (0.00% GC)
  17. --------------
  18. samples: 10000
  19. evals/sample: 1

通过这两篇文章介绍, 我们基本上掌握了 Julia 高性能的方法。 如果还不够, 那就只能求助于并行和分布式了, 等着我们下一篇介绍吧。

欢迎加入知识星球一起分享讨论有趣的技术话题。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19846.html

相关文章

  • 代码飞起——性能Julia学习笔记(三)

    摘要:前面两篇让代码飞起来高性能学习笔记一让代码飞起来高性能学习笔记二,介绍了如何写出高性能的代码,这篇结合我最近的项目,简单测试对比一下各种语言用算法计算的效率。下一篇,我们就来看一下中如何利用并行进一步提高效率。 前面两篇让代码飞起来——高性能 Julia 学习笔记(一) 让代码飞起来——高性能 Julia 学习笔记(二), 介绍了如何写出高性能的 Julia 代码, 这篇结合我最近的项...

    edgardeng 评论0 收藏0
  • 一个神器,写东西快得飞起

    摘要:精准截图不再需要细调截图框这一细节功能真心值无数个赞相比大多数截屏软件只能检测整个应用窗口边界,对界面元素的判定让你操作时可以更加精准快捷,下面的动图就可以让你直观地感受到这个功能的强大之处。接着打开截屏的现象,将里面的显示边框宽度调整为。 ...

    Dionysus_go 评论0 收藏0
  • Julia和Java性能比较

    摘要:介绍性能号称可以赶得上,我很好奇的执行速度,因为我一直用的是,所以就想把和做一下简单的比较。总结从上面的比较来看,确实比快很多,不过这里只做了简单的比较,并没有做严谨的测试,仅供参考。 1、介绍 Julia性能号称可以赶得上c/c++,我很好奇Julia的执行速度,因为我一直用的是Java,所以就想把Julia和Java做一下简单的比较。这次比较一下Julia和Java做一亿次加法运算...

    chengjianhua 评论0 收藏0
  • 这5款Python Web APP开发工具,最中意哪一款?

    摘要:本文汇总了中数款仪表盘工具,简单比较其使用场景学习难度,挑选趁手的来玩即可。进一步学习谁是中最强开发工具谁是中最强开发工具 仪表盘 (Dashboard),可简单的理解为一个交互式网页,在其中,用户可以不懂代码,拖拖拽拽即可与数据交互、做数据探索建模分析、展示自己关注的结果。 ...

    Jingbin_ 评论0 收藏0

发表评论

0条评论

noONE

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<