Hi ... just discovered Nim yesterday ... And was enjoyed to be able to speed up some computation ... But after basic tests ... it seems it's slower than python3 ?!? : how is it possible ?!
For years, I use this algo (brut sudoku resolver) to test speed ...
it's easy/simple to read
so I try to make a nim version ... here it is :
import sets
proc square(g: string,x:int,y:int): string =
let x=int(x/3)*3
let y=int(y/3)*3
g[y*9+x .. y*9+x+2] & g[y*9+x+9 .. y*9+x+11] & g[y*9+x+18 .. y*9+x+20]
proc horiz(g: string, y:int): string =
var ligne: int = y * 9
g[0+ligne .. 8+ligne]
proc vertiz(g: string, x:int): string =
result = ""
for y in 0 .. 8:
var ligne: int = y * 9
result = result & g[x+ligne]
proc freeset(sg: string): HashSet[char] =
toHashSet("123456789.") - toHashSet(sg)
proc interset(g:string,x:int,y:int): HashSet[char] =
freeset(horiz(g,y)) * freeset(vertiz(g,x)) * freeset(square(g,x,y))
proc replace(g:string, pos:int, car: char): string =
g[0..pos-1] & car & g[pos+1..80]
proc resolv(g:string): string =
let i = g.find('.')
if i>=0:
var f = interset(g,i mod 9,int(i/9))
for elem in f:
let ng= resolv( replace(g,i,elem) )
if ng != "": return ng
return ""
else:
return g
var g="""..1....8......45....5.7......273..........2..358.1....2.3.56...9.......1..6.9.7.."""
echo( resolv(g) )
the results are incredible ... py3 is a lot faster (10 times) !!
$ time python3 sudoku.py
741563982639284517825971346492735168167849235358612479273156894984327651516498723
real 0m3,069s
user 0m3,051s
sys 0m0,016s
$ time ./sudoku // NIM compiled
741563982639284517825971346492735168167849235358612479273156894984327651516498723
real 0m30,199s
user 0m30,136s
sys 0m0,024s
Perhaps my nim version is not really optimized ... but, when I see that ... I can't imagine go with nim to speedup things ;-( the python version is a lot more readable and easy to maintain. And 10x faster !
What's wrong ?
And this version runs twice as fast:
proc square(g: string,x:int,y:int): string =
let x=int(x/3)*3
let y=int(y/3)*3
g[y*9+x .. y*9+x+2] & g[y*9+x+9 .. y*9+x+11] & g[y*9+x+18 .. y*9+x+20]
proc horiz(g: string, y:int): string =
var ligne: int = y * 9
g[0+ligne .. 8+ligne]
proc vertiz(g: string, x:int): string =
result = ""
for y in 0 .. 8:
var ligne: int = y * 9
result.add g[x+ligne]
proc freeset(sg: string): set[char] =
var x: set[char] = {}
for c in sg: x.incl c
{'1'..'9', '.'} - x
proc interset(g:string,x:int,y:int): set[char] =
freeset(horiz(g,y)) * freeset(vertiz(g,x)) * freeset(square(g,x,y))
proc replace(g:string, pos:int, car: char): string =
g[0..pos-1] & car & g[pos+1..80]
proc resolv(g:string): string =
let i = g.find('.')
if i>=0:
var f = interset(g,i mod 9,int(i/9))
for elem in f:
let ng= resolv( replace(g,i,elem) )
if ng != "": return ng
return ""
else:
return g
var g="""..1....8......45....5.7......273..........2..358.1....2.3.56...9.......1..6.9.7.."""
import times
let t = getTime()
echo resolv(g), " took: ", getTime() - t
Things are fine, it's easy to speedup Python code with Nim once you invest some time in learning.
thanks !
$ time ./sudoku
741563982639284517825971346492735168167849235358612479273156894984327651516498723
real 0m0,840s
user 0m0,839s
sys 0m0,000s
Now, it's clearly faster ! ... I can consider NIM as an alternative to speedup some things ;-)
I can consider NIM as an alternative to speedup some things
Please also consider to write it as "Nim". Sorry, couldn't resist.
I think rather than acronym/abbreviation it is more about the "grandfather" languages arising at a time when ALL CAPS made output more legible on very low resolution (or not even pixelated!) printers/other devices. There are many examples..FORTRAN, LISP, COBOL, ALGOL, etc. Yes, they are all kind of bacronym's, but ForTran would be the more natural "formula translation" spelling. Then it just became an imperfectly held tradition. That is for PL names "in general" as per xigoi.
One of Nim's influences, Pascal, named after a person, probably stands out as one of the earliest cases bucking this trend (cases that "made it big", anyway).
To haxscramper's shortness point, "Ada" after Lady Ada does not seem to get miswritten much (not that I have detailed statistics on mis-renderings). And then Nim itself is a hybrid of proper name (originally named after King Nimrod), but later "abbreviated" to "Nim", not letter at a time acronym, but by direct truncation. (Abbreviated in scare quotes since naming is fluid enough that it is about as valid to say Araq "renamed" rather than abbreviated from Nimrod to Nim.)
Anyway, it is probably hard to know why people spell it NIM so often.
In other news, I guess the DEBUG BUILD message was not obvious enough. Maybe we should have left in my asterisks after all. ;-)
May we ask, @manatlan how it was you came to "not see" or not act upon that use -d:release message? (Also -d:danger might be even faster and there are other compilation strategies like PGO that may be faster still - or not; it all depends usually.)
Wait , i thought you don't care about NIM or Nim or nim or NiM or N_im or N_i_m or nIM
😜
assert false
echo 1
compile with nim c -d:danger and watch it echo 1. :-) -d:release keeps it.Just to end up on this subject ....
I've made a git: https://github.com/manatlan/sudoku_resolver Which contains the 2 resolvers, py and nim ones
the nim version is (a lot) faster ...
On my old computer, for the 100 first grids :
god this nerd-sniped me.
github.com/shirleyquirk/sudoku_solver
1011 sudokus in 520ms
@shirleyquirk ... Completely amazing ... Your work is astonishing
Could it be the fastest in the world ?! I'm pretty sure it is ...
fastest in the world
oh lordy no. i made the mistake of looking at codegolf stackexchange and they do every 17-clue sudoku in like 200ms
i'm not going to look at the answer to the puzzle before i figure it out tho.