Version française
Home     About     Download     Resources     Contact us    
Browse thread
Why + vs +. but "fake" parametric polymorphism for <
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Basile STARYNKEVITCH <basile@s...>
Subject: Re: [Caml-list] Why + vs +. but "fake" parametric polymorphism for <
Le Thu, Oct 12, 2006 at 02:41:20AM -0300, Carlos Pita écrivait/wrote:
> Umh, soon after writing my previous post I found out an interesting
> chapter in the tutorial giving an exact example of asm code generated by
> the compiler for comparison operator <:
> 
> # let max a b =
>   if a > b then a else b;;
> val max : 'a -> 'a -> 'a = <fun>
> 
> ===>
>         [...]
>         ; Call the C "greaterthan" function (in the OCaml library).
>         pushl   %ebx
>         pushl   %eax
>         movl    $greaterthan, %eax
>         call    caml_c_call
> .L102:
>         addl    $8, %esp
>         ; If the C "greaterthan" function returned 1, jump to .L100
>         [...]
> 
> Pretty expensive for a simple int comparison I would say.


If you explicitly type it as an integer compare 
   let max (a : int) b = if a > b then a else b;;
you get much more efficient code (AMD64 linux ocaml 3.09.2 Debian/Sid)


        .globl  camlMaxi__max_57
camlMaxi__max_57:
.L101:
        cmpq    %rbx, %rax
        jle     .L100
        ret
        .align  4
.L100:
        movq    %rbx, %rax
        ret
        .text

-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France