Created
July 7, 2020 14:07
-
-
Save Vindaar/38b8d2111b2eb80aada9022a33de6c9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import times | |
| import tables | |
| import random | |
| import ggplotnim | |
| import weave | |
| import locks | |
| const runs = 1_000 | |
| const sizes = 1_000 | |
| var lock: Lock | |
| lock.initLock() | |
| proc oneSizeRun(size: int): DataFrame = | |
| var myTable = initTable[int, int]() | |
| var myList: seq[int] | |
| for i in 0 .. size: | |
| myTable[i] = i | |
| myList.add(i) | |
| myList.shuffle() | |
| var dummyCounter = 0 | |
| var listTimes = newSeq[float](runs + 1) | |
| var tableTimes = newSeq[float](runs + 1) | |
| for n in 0 .. runs: | |
| const trytimes = 10 | |
| var start = epochTime() | |
| for j in 0 .. trytimes: | |
| for i in 0 ..< n: | |
| if myList.contains(i): | |
| inc dummyCounter | |
| let listTime = epochTime() - start | |
| start = epochTime() | |
| for j in 0 .. trytimes: | |
| for i in 0 ..< n: | |
| if myTable.hasKey(i): | |
| inc dummyCounter | |
| let tableTime = epochTime() - start | |
| listTimes[n] = listTime/trytimes | |
| tableTimes[n] = tableTime/trytimes | |
| echo dummyCounter | |
| result = seqsToDf({ "list" : listTimes, | |
| "table" : tableTimes, | |
| "runs" : arange(0, runs + 1) }) | |
| .gather(["table", "list"], key = "type", value = "time") | |
| result["size"] = constantColumn(size, result.len) | |
| init(Weave) | |
| var dfs = newSeq[DataFrame](sizes + 1) | |
| var dfBuf = cast[ptr UncheckedArray[DataFrame]](dfs[0].addr) | |
| #parallelFor i in 0 .. sizes: | |
| # captures: {dfBuf} | |
| # dfBuf[i] = spawn oneSizeRun(i) | |
| parallelFor i in 0 .. sizes: | |
| captures: {dfBuf} | |
| dfBuf[i] = oneSizeRun(i) | |
| #let res = df.addr # For mutation we need to capture the address. | |
| #parallelForStaged i in 0 .. sizes: | |
| # captures: {res} | |
| # prologue: | |
| # var localDf = newDataFrame() | |
| # loop: | |
| # localDf = oneSizeRun(i * 5) | |
| # epilogue: | |
| # echo "Thread ", getThreadID(Weave), ": localDf ", localDf | |
| # lock.acquire() | |
| # res[].add localDf | |
| # lock.release() | |
| syncRoot(Weave) | |
| var df = newDataFrame() | |
| for i in 0 .. sizes: | |
| echo df | |
| let toadd = dfBuf[i] | |
| echo toadd.len | |
| echo getKeys(toadd) | |
| echo toAdd | |
| echo "////////////////////////////////" | |
| df.add toadd | |
| exit(Weave) | |
| #echo df.pretty(-1) | |
| echo df | |
| #ggplot(df, aes("runs", "time", color = "type")) + | |
| # geom_line() + | |
| # ggsave("mini_bench.pdf") | |
| ggplot(df, aes("runs", "size", fill = "time")) + | |
| geom_raster(aes = aes(height = 5, width = 1)) + | |
| scale_x_continuous() + | |
| facet_wrap("type") + | |
| ggsave("bench_heatmap.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment