首页 > 解决方案 > Union 的 Julia NTuple 需要指数级时间/内存来编译?

问题描述

我正在制作一个相对较大的 NTuple,其中的值有时可能什么都不是。我注意到构建我的结构需要很长时间。仔细观察,似乎 NTuple 需要指数级的时间和 RAM 来构建。为什么是这样?

NTuple{2, Union{Int8, Nothing}}

降低到

Union{Tuple{Int8, Int8}, Tuple{Int8, Nothing}, Tuple{Nothing, Int8}, Tuple{Nothing, Nothing}}`

?

如果是这样,为什么?

供参考,一些时间:

julia> for i in 1:100
         @time NTuple{i, Union{Int8, Nothing}}(Tuple(1:i))
       end
  0.006849 seconds (11.18 k allocations: 551.179 KiB)
  0.010944 seconds (19.20 k allocations: 925.659 KiB)
  0.010264 seconds (23.84 k allocations: 1.090 MiB)
  0.011766 seconds (29.47 k allocations: 1.351 MiB)
  0.012595 seconds (34.73 k allocations: 1.566 MiB)
  0.014412 seconds (40.18 k allocations: 1.795 MiB)
  0.017048 seconds (47.26 k allocations: 2.096 MiB)
  0.019018 seconds (57.91 k allocations: 2.559 MiB)
  0.029885 seconds (76.22 k allocations: 3.373 MiB, 14.82% gc time)
  0.056189 seconds (111.05 k allocations: 4.945 MiB)
  0.063619 seconds (181.21 k allocations: 8.135 MiB)
  0.112901 seconds (326.06 k allocations: 14.735 MiB, 2.65% gc time)
  0.215413 seconds (631.22 k allocations: 28.683 MiB, 0.92% gc time)
  0.442667 seconds (1.27 M allocations: 58.127 MiB, 1.08% gc time)
  0.950875 seconds (2.63 M allocations: 120.167 MiB, 0.85% gc time)
  2.064045 seconds (5.55 M allocations: 254.885 MiB, 0.90% gc time)
  4.472772 seconds (11.62 M allocations: 533.487 MiB, 0.74% gc time)
  9.727991 seconds (24.34 M allocations: 1.091 GiB, 0.73% gc time)
 22.080309 seconds (50.95 M allocations: 2.285 GiB, 0.63% gc time)
 48.423639 seconds (106.53 M allocations: 4.777 GiB, 0.76% gc time)
 90.654842 seconds (222.40 M allocations: 9.973 GiB, 0.78% gc time)

标签: typestuplesjulia

解决方案


推荐阅读