[Shootout-list] Mandelbrot Set for CMUCL and SBCL 2nd try

Pascal Obry pascal@obry.net
Wed, 30 Mar 2005 13:07:46 +0200


Yannick,

 > As Pascal said, the other benchmarks achieved an output similar to the
 > C one at the cost of doing it the C way instead of doing it the way
 > they would normally do it.  

I think so indeed. Let's take and example based on the Ada bench. The current
implementation look like:

   for Y in 0 .. Height - 1 loop
      for X in 0 .. Width - 1 loop
         Zr := 0.0;
         Zi := 0.0;
         Cr := 2.0 * Real (X) / Real (Width) - 1.5;
         Ci := 2.0 * Real (Y) / Real (Height) - 1.0;

         Limit_Reached := False;

         for I in 1 .. Iter loop
            Tr  := Zr * Zr - Zi * Zi + Cr;
            Ti  := 2.0 * Zr * Zi + Ci;
            Zr  := Tr;
            Zi  := Ti;

            if Zr * Zr + Zi * Zi > Limit then
               Limit_Reached := True;
               exit;
            end if;
         end loop;

         if Limit_Reached then
            Byte_Acc := Shift_Left (Byte_Acc, 1) or 16#00#;
         else
            Byte_Acc := Shift_Left (Byte_Acc, 1) or 16#01#;
         end if;

I wanted to introduced Zr2 = Zr ** 2; and Zi2 = Zi ** 2; The code would have
looked something like:

   for Y in 0 .. Height - 1 loop
      for X in 0 .. Width - 1 loop
         Zr := 0.0;
         Zi := 0.0;
         Cr := 2.0 * Real (X) / Real (Width) - 1.5;
         Ci := 2.0 * Real (Y) / Real (Height) - 1.0;

         Limit_Reached := False;

         for I in 1 .. Iter loop
            Zr2 := Zr ** 2;
            Zi2 := Zi ** 2;
            Tr  := Zr2 - Zi2 + Cr;
            Ti  := 2.0 * Zr * Zi + Ci;
            Zr  := Tr;
            Zi  := Ti;

            exit when Zr2 + Zi2 > Limit;
         end loop;

         if Zr2 + Zi2 > Limit then
            Byte_Acc := Shift_Left (Byte_Acc, 1) or 16#00#;
         else
            Byte_Acc := Shift_Left (Byte_Acc, 1) or 16#01#;
         end if;

This is cleaner and faster. But it does not gives the same output (only 2
bytes are different).

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595