Re: [閒聊] AppendTo 指令的效率替代方案
剛剛試了一下G大的建議, 意外的發現linkedlist是存reference...這樣的話好像就不用
做NL compress了
測試如下
Module[{seed, mem, bag},
RandomMem2[in__] := (mem = {mem, bag[in]}; RandomVariate[in]);
ExtractSeed2[] := {seed, mem = Flatten@mem};
SetSeed2[InMem_List: {}] := SetSeed2[RandomInteger@(2^64), InMem];
SetSeed2[InSeed__, InMem_List: {}] := (mem = {};
SeedRandom[seed = InSeed]; (RandomMem2@##;) & @@@ InMem;);
SetSeed2[];]
Module[{seed, mem, temp, store, bag},
RandomMem[1, in___] := (mem = {mem, bag[1, in]};
RandomVariate[temp, in]);
RandomMem[dist_,
in___] := (mem = {mem,
bag[Which[dist === temp, 1, True, temp = dist], in]};
RandomVariate[dist, in]);
ExtractSeed[] := {seed, mem = Flatten@mem};
SetSeed[InMem_List: {}] := SetSeed[RandomInteger@(2^64), InMem];
SetSeed[InSeed__, InMem_List: {}] := (mem = temp = {};
SeedRandom[seed = InSeed]; (RandomMem@##;) & @@@ InMem;);
SetSeed[];]
dist = KernelMixtureDistribution@RandomReal[{-1, 1}, 1000]; dist2 =
Table[KernelMixtureDistribution@RandomReal[{-1, 1}, 1000], {200}];
Print@TableForm[#,
TableHeadings -> {{"Gaussian", "2 Alternating", "Custom 1000pts",
"200 Customs"}, {"RandomVariate", "RandomMem(nextline)",
"RandomMem2(no compress)"}, {"Time(s)", "memory(B/item)"}}] &[
Table[ClearSystemCache[]; SetSeed[0]; SetSeed2[0];
temp = #2^-1 MemoryInUse[];
AbsoluteTiming[Do[F@#1;, {i, #2}];
N[#2^-1 MemoryInUse[] - temp]], {F, {RandomVariate[#, 2] &,
RandomMem[#, 2] &,
RandomMem2[#, 2] &}}] & @@@ {{NormalDistribution[0, 2],
10^5}, {NormalDistribution[0, 2 + Mod[i, 2]], 10^5}, {dist,
10^4}, {Unevaluated[dist2[[Mod[i, 200] + 1]]],
10^4}}]; ClearSystemCache[]; Print["Time for ExtractSeed \
(RandomMem(nextline)): ",
AbsoluteTiming[ExtractSeed[];][[
1]], "\nTime for ExtractSeed2 (RandomMem2(no compress)): ",
AbsoluteTiming[ExtractSeed2[];][[
1]], "\nmemory for bag[1,2] (B/item): ",
ByteCount[{bag[1, 2]}], "\nmemory for bag[Gaussian,2] (B/item): ",
ByteCount[{bag[NormalDistribution[0, 3],
2]}], "\nmemory for bag[dist,2] (B/item): ",
ByteCount[{bag[dist, 2]}]]; ClearAll[dist, dist2, temp, bag]
測出來每次call大概都只多112B (光{bag[]}本身就88B)
結論: 最簡單的最好~ 直接存就對了LOL (不過還是蠻貴的就是,每次call要多~20us)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.23.123
※ 文章網址: https://www.ptt.cc/bbs/Mathematica/M.1476360388.A.9A8.html
推
10/13 23:48, , 1F
10/13 23:48, 1F
推
10/15 21:51, , 2F
10/15 21:51, 2F
推
10/16 17:46, , 3F
10/16 17:46, 3F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
Mathematica 近期熱門文章
PTT數位生活區 即時熱門文章