terminus-community / discussing-code

Repository for your code snippets that you would like to share and discuss with others
0 stars 0 forks source link

queens.hs works too slowly #1

Open prozion opened 3 years ago

prozion commented 3 years ago

https://en.wikipedia.org/wiki/Eight_queens_puzzle The problem is to put 8 queens in such positions on the chessboard that they don't threaten each other. I use to write this program for every programming language I learn (on the basic level). Here I wonder, what could be done to make this code work faster. And first of all, how to understand what slows down the calculation? Which tools people usually use in Haskell environment to detect problematic fragments of the code?

iokasimov commented 3 years ago

@prozion I think this is the most easiest way if you don't use cabal or stack: https://wiki.haskell.org/How_to_profile_a_Haskell_program

But since it's simple pure code it should be enough to check algorithmic complexity. Just on the first sign there are several places in your code with list operations that look terrible in terms of Big O notation.

iokasimov commented 3 years ago

Also, as far as I understand, you use naive algorithm with generation of intermediate lists (immutability should suffer here) and findQueenConfigurations is not tail-recursive. I think it should be faster with backtracking algorithm which is not the easiest way though. I hope I will find time one day to investigate it because it can be implemented with continuations.