In pursuit of optimizing the following code, I realized that if somehow the
loop is paralleled (i.e. each thread operates on a certain chunk of the array),
the code may execute faster:
import tables, os, parseutils, strutils
proc ytz(args: openArray[string]): uint =
var
table = initTable[uint, uint]()
n: uint
for numStr in args:
if parseUInt(numStr, n) == 0:
quit("Parse error: " & numStr)
table.mgetOrPut(n, 0) += n # here
if table[n] > result: # here
result = table[n] # and here
proc main() =
echo(paramStr(1).open().readAll().splitLines().ytz())
main()
Run
However I need to access the table (both mutably and immutably) in the places I
pointed out. Is there any way I can leverage multiple cores of the target
system to maximize efficiency? As far as I know Java has concurrent hashmap but
I never used it myself.