Быстрый и экономичный конечный цикл на хаскеле (ghc-6.8.2)...
loopn n m | n == 1 = m
| n == 2 = m >> m
| n == 3 = m >> m >> m
| n > 3 = iloopn
where
r = n .&. 3
d = n `shiftR` 2
tm = loopn d (m >> m >> m >> m)
iloopn | r == 0 = tm
| r == 1 = m >> tm
| r == 2 = m >> m >> tm
| r == 3 = m >> m >> m >> tm
main = loopn 10000000 (return 0)
./a.out +RTS -sstderr
23,720 bytes allocated in the heap
552 bytes copied during GC (scavenged)
0 bytes copied during GC (not scavenged)
32,768 bytes maximum residency (1 sample(s))
1 collections in generation 0 ( 0.00s)
1 collections in generation 1 ( 0.00s)
1 Mb total memory in use
INIT time 0.00s ( 0.00s elapsed)
MUT time 0.09s ( 0.12s elapsed)
GC time 0.00s ( 0.00s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 0.09s ( 0.12s elapsed)
%GC time 0.0% (0.1% elapsed)
Alloc rate 254,160 bytes per MUT second
Productivity 100.0% of total user, 80.2% of total elapsed
Не, ну разве это может быть быстрее последовательного на таких маленьких числах?