another fpc vs gcc benchmark lazarus / 62-General ---------------------------------------------------------------------- fcu · 2017-09-13 16:22 Hi this is just for fun . all i want to know is where is the slowness is coming from ? when i translate the c to pas i thought the speed would be the same , because there is no much calculation  but ok the result on my machine (win7 32bits , intel g2020 ) fpc : 140 fps gcc : 335 fps pascal : compiled with -O3 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program demo;{$ifdef FPC}{$mode objfpc}{$h+}{$endif}{$ifdef mswindows}{$apptype gui}{$endif}uses  sdl,math;        const  s_w = 1024;  s_h = 640; var  event : TSDL_Event;  done : boolean = false;  screen : PSDL_Surface;    myLut  : array[0..s_w*s_h-1] of longword;  myTex  : array[0..256*256-1  ] of longword;  count,tickf,tickl,fps : longword;  dif : single; function inttostr(v : integer): ansistring; inline;begin  str(v,result);end; procedure initLut();var i,j : longint;    x,y,w,r,a,u,v : single;    iu,iv,iw : int32;begin        for j:=0 to s_h -1 do    for i:=0 to s_w -1 do    begin        x := -1.0 + i*(2.0/s_w);        y :=  1.0 - j*(2.0/s_h);        r := sqrt( x*x+y*y );        a := arctan2( y, x );         u := 1.0/r;        v := a*(3.0/3.14159);        w := r*r;        if( w>1.0  ) then w := 1.0;         iu := round(u*255.0);        iv := round(v*255.0);      iw := round(w*255.0);         myLut[s_w*j+i] := ((iw and 255)<<16) or ((iv and 255)<<8) or (iu and 255);    end;end; procedure initTexture();var i,j,r,g : integer;begin        for j:=0 to 256-1 do    for i:=0 to 256-1 do    begin        r := (i xor j);        g := (((i>>6) and 1) xor ((j>>6) and 1))*255;        g := (g*5 + 3*r)>>3;      myTex[256*j+i] := $ff000000 or (g<<16) or (g<<8) or g;    end;end; procedure intro_init();begin        initTexture();    initLut();end; function colScale( col : longword;sca : integer ):longword; begin    result := ((((col and $00ff00ff)*sca)>>8) and $00ff00ff) +              ((((col and $0000ff00)*sca)>>8) and $0000ff00);end; procedure do_intro( buffer : plongword; itime : longint ); var i , val, col : integer;begin    for i:=0 to s_w*s_h-1 do    begin        val := myLut[i];            col := myTex[ ((val and $0000ffff)+(itime>>2)) and $0000ffff ];        buffer[i] := colScale( col, (val>>16) );    end;end; begin                SDL_Init(SDL_INIT_VIDEO);        screen := SDL_SetVideoMode( s_w, s_h, 32 , SDL_SWSURFACE );        intro_init();        tickf := SDL_Getticks();            while not done  do        begin      dif := (tickl-tickf) / 1000;      if (dif >= 1.0 ) then      begin        tickf := tickl;        fps := count;        count := 0;            SDL_WM_SetCaption(pchar(inttostr(fps)+' fps'),nil);      end      else      begin        inc(count);      end;                while  SDL_PollEvent( @event ) > 0  do            case event.type_  of          SDL_KEYDOWN:                if ( event.key.keysym.sym = SDLK_ESCAPE ) then                begin                  done := true;                end;                SDL_QUITEV:                 begin                  done := true;                end;              end;          do_intro(screen^.pixels,SDL_Getticks());          tickl := SDL_GetTicks();          SDL_Flip(screen);        end;                  SDL_FreeSurface(screen);end.   gcc : also compiled with -O3 Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #include #include  const int s_w = 1024;const int s_h = 640;  static unsigned int myLut[s_w*s_h];static unsigned int myTex[256*256]; void initLut( void ){        for( int j=0; j1.0f ) w=1.0f;         const int iu = (int)(u*255.0f);        const int iv = (int)(v*255.0f);        const int iw = (int)(w*255.0f);         myLut[s_w*j+i] = ((iw&255)<<16) | ((iv&255)<<8) | (iu&255);    }}  void initTexture( void ){        for( int j=0; j<256; j++ )    for( int i=0; i<256; i++ )    {        int r = (i ^ j);        int g = (((i>>6)&1)^((j>>6)&1))*255;        g = (g*5 + 3*r)>>3;        myTex[256*j+i] = 0xff000000 | (g<<16) | (g<<8) | g;    }}void intro_init( void ){        initTexture();    initLut();}  unsigned int colScale( unsigned int col, int sca ){    return ((((col&0x00ff00ff)*sca)>>8)&0x00ff00ff) + ((((col&0x0000ff00)*sca)>>8)&0x0000ff00); }void do_intro( unsigned int *buffer, long itime ){    for( int i=0; i>2))&0x0000ffff ];        buffer[i] = colScale( col, (val>>16) );    }} int main ( int argc, char** argv ){    unsigned int tickf,tickl = 0,fps,count = 0;    float dif;    char buf[32];    SDL_Event event;      SDL_Init( SDL_INIT_VIDEO );    atexit(SDL_Quit);     SDL_Surface* screen = SDL_SetVideoMode(s_w, s_h, 32, SDL_SWSURFACE );    intro_init();     bool done = false;    tickf = SDL_GetTicks();    while (!done)    {     dif = (tickl-tickf) / 1000;     if (dif >= 1.0f ){        tickf = tickl;        fps = count;        count = 0;        sprintf(buf,"%d fps",fps);        SDL_WM_SetCaption(buf,NULL);     } else {       count++;     };      while (SDL_PollEvent(&event))      {        switch (event.type)      {          case SDL_QUIT:            done = true;            break;        case SDL_KEYDOWN:            if ( event.key.keysym.sym == SDLK_ESCAPE ){              done = true;            }          break;        }      }      do_intro((unsigned int *)screen->pixels, SDL_GetTicks());     SDL_Flip(screen);     tickl = SDL_GetTicks();    }     SDL_FreeSurface(screen);    return 0;} ---------------------------------------------------------------------- Nitorami · 2017-09-13 17:03 The bottleneck seems to be in your function colScale. I get a factor 2 speed increase when exchanging all occurences of longword in the code by longint. This may be a penalty of mixing signed and unsigned types; AFAIK pascal is stricter than C here. ---------------------------------------------------------------------- fcu · 2017-09-13 17:12 yes thats true , thanks also by inline colScale function i get the same speed now . thanks again