Re: [閒聊] AppendTo 指令的效率替代方案

看板Mathematica作者 (Amiba Gelos)時間8年前 (2016/10/13 20:06), 編輯推噓3(300)
留言3則, 2人參與, 最新討論串2/2 (看更多)
剛剛試了一下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
這個是在計算各種結構的內部資料量 (ByteCount 函數)
10/15 21:51, 2F

10/16 17:46, , 3F
這程式給我感覺像跳學中會時,知道每個數字,但不知做啥XD
10/16 17:46, 3F
文章代碼(AID): #1N_tZ4ce (Mathematica)
文章代碼(AID): #1N_tZ4ce (Mathematica)