@yglukhov: Ah, you don't make copies of the entries of Directions. I think
that's an optimization the compiler should be able to do somehow. Also, you can
use `0 .. Directions.high` instead of `0 ..< Directions.len`. Using `mitems`
instead achieves the same effect, but needs a `var`:
# Nim example to test execution speed against a Python version
import times, os
const
board = ['0', 'p', '.', 'p', 'P', '.', 'p', 'P', '.', 'p', '.', 'p', 'P',
'.', 'p']
NE = [8, 2, 13, 4, 5, 7, 8, 3, 9, 12, 7, 8, 3, 9, 12]
var Directions = [NE, NE, NE, NE] # for short: in real application the
array elements differ
proc do_something(board: openarray[char]): seq[char] =
result = newSeqOfCap[char](board.len * Directions.len)
for i in 0 .. board.high:
for d in Directions.mitems:
result.add board[d[i]]
let t0 = cpuTime()
var res: seq[char]
const count = 100_000
for i in 1..count:
res = do_something(board)
let t1 = cpuTime()
echo "***** Time elapsed for Nim: ", t1 - t0, " Counts: ", count
echo res.repr