Skip to content

Instantly share code, notes, and snippets.

@chrisdone-artificial
Last active November 19, 2024 21:22
Show Gist options
  • Save chrisdone-artificial/46a1b451246e15efa69f05bb5f5bbebb to your computer and use it in GitHub Desktop.
Save chrisdone-artificial/46a1b451246e15efa69f05bb5f5bbebb to your computer and use it in GitHub Desktop.
Ackermann in Hell vs GHCi

GHCi:

ghci> main
4093
it :: ()
(4.05 secs, 3,864,385,304 bytes)

Hell:

bash-3.2$ cabal run -- hell ack.hell +RTS -s
4093
   8,459,137,800 bytes allocated in the heap
     231,080,600 bytes copied during GC
         685,144 bytes maximum residency (12 sample(s))
         400,296 bytes maximum slop
              54 MiB total memory in use (0 MiB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0      1965 colls,  1965 par    0.416s   0.395s     0.0002s    0.0008s
  Gen  1        12 colls,    11 par    0.010s   0.005s     0.0004s    0.0009s

  Parallel GC work balance: 1.74% (serial 0%, perfect 100%)

  TASKS: 22 (1 bound, 21 peak workers (21 total), using -N10)

  SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)

  INIT    time    0.005s  (  0.005s elapsed)
  MUT     time    3.021s  (  2.462s elapsed)
  GC      time    0.426s  (  0.400s elapsed)
  EXIT    time    0.001s  (  0.010s elapsed)
  Total   time    3.453s  (  2.877s elapsed)

  Alloc rate    2,799,955,182 bytes per MUT second

  Productivity  87.5% of total user, 85.6% of total elapsed

By the way, dropping the tuples shaves a bit of time, but Hell still comes out on top:

ghci> main
4093
it :: ()
(3.52 secs, 2,967,106,944 bytes)

vs

  Total   time    3.221s  (  2.683s elapsed)

The numbers are fluctuate by +/- 10ms, so: very stable.

ackermann = Function.fix (\ack -> \(m, n) ->
if Int.eq m 0 then Int.plus n 1
else if Int.eq n 0 then ack (Int.subtract 1 m, 1)
else ack (Int.subtract 1 m, ack (m, Int.subtract 1 n)))
main = IO.print $ Main.ackermann (3, 9)
import Control.Monad.Fix
ackermann :: (Int, Int) -> Int
ackermann = fix (\ack (m, n) ->
if m == 0 then n + 1
else if n == 0 then ack (m - 1, 1)
else ack (m - 1, ack (m, n - 1)))
main = print $ ackermann (3, 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment