News for hackers. Stuff that matters.
Re:So You Want to Write Your Own Language (2014) by wruza on Saturday May 20, 2017 @12:33AM (#14381055)
(From article comments)

    #include <stdio.h>
    
    int
    main(int argc, char **argv)
    {
      double i, s;
      s = 0;
      for (i = 1; i < 100000000; i++)
        s += 1/i;
      printf("Almost infinity is %g\n", s);
    }

    Lennarts-Computer% gcc -O3 inf.c -o inf
    Lennarts-Computer% time ./inf
    Almost infinity is 18.9979
    1.585u 0.009s 0:01.62 97.5%     0+0k 0+0io 0pf+0w
And now the Haskell code:

    import BASIC
    main = runBASIC' $ do
        10 LET I =: 1
        20 LET S =: 0
        30 LET S =: S + 1/I
        40 LET I =: I + 1
        50 IF I <> 100000000 THEN 30
        60 PRINT "Almost infinity is"
        70 PRINT S
        80 END
And running it:

    Lennarts-Computer% ghc --make Main.hs
    [4 of 4] Compiling Main             ( Main.hs, Main.o )
    Linking Main ...
    Lennarts-Computer% ./Main
    Almost infinity is
    18.9979
    CPU time:   1.57s
As you can see it's about the same time. In fact the assembly code for the loops look pretty much the same. Here's the Haskell one:

    LBB1_1: ## _L4
            movsd   LCPI1_0, %xmm2
            movapd  %xmm1, %xmm3
            addsd   %xmm2, %xmm3
            ucomisd LCPI1_1, %xmm3
            divsd   %xmm1, %xmm2
            addsd   %xmm2, %xmm0
            movapd  %xmm3, %xmm1
            jne     LBB1_1  ## _L4
---

OMG that is astonishing

There are no comments on this comment yet.