Skip to content

Create a fast operative for 'for' #433

@masak

Description

@masak

I wrote the following straightforward code that prints a chessboard:

(set N 13)

(set board (array (list N N) \.))
(pr "ready now to print board" \lf)

(pr "  ")
(for i 1 N
  (pr " " (nchar (+ i 96))))
(pr \lf)

(for i 1 N
  (for j 2 i
    (pr " "))
  (when (< i 10)
    (pr " "))
  (pr i " ")
  (for j 1 N
    (pr " " (board i j)))
  (pr \lf))

(pr \lf)

Guess how long it takes to run? 28 minutes!

% time bel hex-board.bel
ready now to print board
   a b c d e f g h i j k l m
 1  . . . . . . . . . . . . .
  2  . . . . . . . . . . . . .
   3  . . . . . . . . . . . . .
    4  . . . . . . . . . . . . .
     5  . . . . . . . . . . . . .
      6  . . . . . . . . . . . . .
       7  . . . . . . . . . . . . .
        8  . . . . . . . . . . . . .
         9  . . . . . . . . . . . . .
         10  . . . . . . . . . . . . .
          11  . . . . . . . . . . . . .
           12  . . . . . . . . . . . . .
            13  . . . . . . . . . . . . .

bel hex-board.bel  1673.68s user 2.84s system 99% cpu 27:59.15 total

Just now I also created a version of the script containing only the for loops. I'm running it now, timing it. The fact that I'm still typing these words as it continues to run shows how unacceptably slow for is right now.

(set N 13)

(for i 1 N)

(for i 1 N
  (for j 2 i)
  (for j 1 N))

I don't foresee any big issue in creating a fast operative for for. #412 doesn't mention it among the ones it decides to skip because it's hard.

Ah, here:

% time bel only-for-hex-board.bel
bel only-for-hex-board.bel  188.32s user 0.66s system 99% cpu 3:09.80 total

Three minutes. Yes, that would still be a really nice time win. Seems that would still leave 25 minutes for my hex board, but at least it wouldn't be the for loops' fault.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions