Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Interesting optimization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Interesting optimization
On Thu, 22 Jul 2004, John Prevost wrote:

> let update'' c =
>   let rec loop c' a =
>     if a >= 0
>       then loop (c' land 0xff) (succ a)
>       else c'
>   in
>   c := (loop !c 0)


let update''' c =
    let rec loop c i =
        let c = c land 0xff in
        if (i < max_int) then
            loop c (i+1)
        else
            c
    in
    loop c 0
;;

Haven't tried timing it.  But the core code on the x86 becomes:
Temp__loop_59:
.L101:
        andl    $511, %eax
        movl    $2147483647, %ecx
        cmpl    %ecx, %ebx
        jge     .L100
        addl    $2, %ebx
        jmp     .L101
        .align  16

I'm slightly disappointed that Ocaml didn't just
	cmpl	$2147483647, %ebx
and not use ecx at all.  Not that this is that big a problem.  The only 
other possible optimizations would be to recognize that the entire loop is 
pointless, and the function can be replaced by:
let update c = c land 0xff;;

I feel comfortable leaving that particular optimization in the hands of 
the programmer, however.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners