Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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)
    loop c 0

Haven't tried timing it.  But the core code on the x86 becomes:
        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 

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: