Difference between revisions of "Talk:Improving language shootout results"

From Lazarus wiki
Jump to navigationJump to search
(Suggestion for improvement.)
 
m (code formatting)
 
Line 3: Line 3:
 
The most important tricks are conversion of the multiplication for Cx and Cy in the loops into additions, actually manually in line merging the inlined function and save some assignements there. A real boost would be to use 128 bit xmm registers and instructions for  Cx, Cy, Zr, Zi, Tr, Ti as well as threads for the quad core shootout, as some of the competitors do.
 
The most important tricks are conversion of the multiplication for Cx and Cy in the loops into additions, actually manually in line merging the inlined function and save some assignements there. A real boost would be to use 128 bit xmm registers and instructions for  Cx, Cy, Zr, Zi, Tr, Ti as well as threads for the quad core shootout, as some of the competitors do.
  
program mandelbrot2;
+
<code>
 
+
program mandelbrot2;
var
+
  n: longint;
+
var
  TextBuf: array[0..$FFF] of byte;
+
  n: longint;
  OutFile: PText;
+
  TextBuf: array[0..$FFF] of byte;
 
+
  OutFile: PText;
procedure run;
+
const
+
procedure run;
  Limit: double = 4.0;
+
const
  two: double = 2.0;
+
  Limit: double = 4.0;
var
+
  two: double = 2.0;
  i, index1, index2, bits, bit: longint;
+
var
  Zr, Zi, Ti, Tr: double;
+
  i, index1, index2, bits, bit: longint;
  Cx, Cy, Step: double;
+
  Zr, Zi, Ti, Tr: double;
begin
+
  Cx, Cy, Step: double;
  Step := two/n;
+
begin
  Cy := -1.0;
+
  Step := two/n;
  for index1 := 1 to n do
+
  Cy := -1.0;
  begin
+
  for index1 := 1 to n do
    Cx := -1.5;
+
  begin
    bits := 255;
+
    Cx := -1.5;
    bit  := 128;
+
    bits := 255;
    for index2 := 1 to n do
+
    bit  := 128;
    begin
+
    for index2 := 1 to n do
      Ti := Cy * Cy;
+
    begin
      Tr := Cx * Cx;
+
      Ti := Cy * Cy;
      if (Tr + Ti >= limit) then
+
      Tr := Cx * Cx;
        bits := bits xor bit
+
      if (Tr + Ti >= limit) then
      else
+
        bits := bits xor bit
      begin
+
      else
        Zi := (Cx + Cx + 1.0) * Cy;
+
      begin
        Zr := Tr - Ti + Cx;
+
        Zi := (Cx + Cx + 1.0) * Cy;
        Ti := Zi * Zi;
+
        Zr := Tr - Ti + Cx;
        Tr := Zr * Zr;
+
        Ti := Zi * Zi;
        if (Tr + Ti >= limit) then
+
        Tr := Zr * Zr;
          bits := bits xor bit
+
        if (Tr + Ti >= limit) then
        else
+
          bits := bits xor bit
        begin
+
        else
          for i := 3 to 50 do
+
        begin
          begin
+
          for i := 3 to 50 do
            Zi := Zr*Zi + Zr*Zi + Cy;
+
          begin
            Zr := Tr - Ti + Cx;
+
            Zi := Zr*Zi + Zr*Zi + Cy;
            Ti := Zi * Zi;
+
            Zr := Tr - Ti + Cx;
            Tr := Zr * Zr;
+
            Ti := Zi * Zi;
            if (Tr + Ti >= limit) then
+
            Tr := Zr * Zr;
            begin
+
            if (Tr + Ti >= limit) then
              bits := bits xor bit;
+
            begin
              break;
+
              bits := bits xor bit;
            end;
+
              break;
          end;
+
            end;
        end;
+
          end;
      end;
+
        end;
      if bit > 1 then
+
      end;
        bit := bit shr 1
+
      if bit > 1 then
      else
+
        bit := bit shr 1
      begin
+
      else
        write(OutFile^, chr(bits));
+
      begin
        bits := 255;
+
        write(OutFile^, chr(bits));
        bit  := 128;
+
        bits := 255;
      end;
+
        bit  := 128;
      Cx := Cx + Step;
+
      end;
    end;
+
      Cx := Cx + Step;
    if bit < 128 then
+
    end;
      write(OutFile^, chr(bits xor ((bit shl 1) - 1)));
+
    if bit < 128 then
    Cy := Cy + Step;
+
      write(OutFile^, chr(bits xor ((bit shl 1) - 1)));
  end;
+
    Cy := Cy + Step;
end;
+
  end;
 
+
end;
begin
+
 
  OutFile := @Output;
+
begin
  SetTextBuf(OutFile^, TextBuf);
+
  OutFile := @Output;
 
+
  SetTextBuf(OutFile^, TextBuf);
  Val(ParamStr(1), n);
+
  writeln(OutFile^, 'P4');
+
  Val(ParamStr(1), n);
  writeln(OutFile^, n,' ',n);
+
  writeln(OutFile^, 'P4');
 
+
  writeln(OutFile^, n,' ',n);
  run;
+
end.
+
  run;
 +
end.
 +
</code>

Latest revision as of 20:02, 2 June 2009

This gives about 15% improvement on Mac OS X 10.5, fpc 2.2.4 over the present version in the shootout:

The most important tricks are conversion of the multiplication for Cx and Cy in the loops into additions, actually manually in line merging the inlined function and save some assignements there. A real boost would be to use 128 bit xmm registers and instructions for Cx, Cy, Zr, Zi, Tr, Ti as well as threads for the quad core shootout, as some of the competitors do.

program mandelbrot2;

var
  n: longint;
  TextBuf: array[0..$FFF] of byte;
  OutFile: PText;

procedure run;
const
  Limit: double = 4.0;
  two: double = 2.0;
var
  i, index1, index2, bits, bit: longint;
  Zr, Zi, Ti, Tr: double;
  Cx, Cy, Step: double;
begin
  Step := two/n;
  Cy := -1.0;
  for index1 := 1 to n do
  begin
    Cx := -1.5;
    bits := 255;
    bit  := 128;
    for index2 := 1 to n do
    begin
      Ti := Cy * Cy;
      Tr := Cx * Cx;
      if (Tr + Ti >= limit) then
        bits := bits xor bit
      else
      begin
        Zi := (Cx + Cx + 1.0) * Cy;
        Zr := Tr - Ti + Cx;
        Ti := Zi * Zi;
        Tr := Zr * Zr;
        if (Tr + Ti >= limit) then
          bits := bits xor bit
        else
        begin
          for i := 3 to 50 do
          begin
            Zi := Zr*Zi + Zr*Zi + Cy;
            Zr := Tr - Ti + Cx;
            Ti := Zi * Zi;
            Tr := Zr * Zr;
            if (Tr + Ti >= limit) then
            begin
              bits := bits xor bit;
              break;
            end;
          end;
        end;
      end;
      if bit > 1 then
        bit := bit shr 1
      else
      begin
        write(OutFile^, chr(bits));
        bits := 255;
        bit  := 128;
      end;
      Cx := Cx + Step;
    end;
    if bit < 128 then
      write(OutFile^, chr(bits xor ((bit shl 1) - 1)));
    Cy := Cy + Step;
  end;
end;
 
begin
  OutFile := @Output;
  SetTextBuf(OutFile^, TextBuf);

  Val(ParamStr(1), n);
  writeln(OutFile^, 'P4');
  writeln(OutFile^, n,' ',n);

  run;
end.