ShortInt computations appear slow - is there a way to force memory alignment
Original Reporter info from Mantis: crlab @neurolabusc1
-
Reporter name: Chris Rorden
Original Reporter info from Mantis: crlab @neurolabusc1
- Reporter name: Chris Rorden
Description:
I thought I would see if there is an explanation for the phenomena described at:
http://www.lazarus.freepascal.org/index.php?name=PNphpBB2&file=viewtopic&t=453
FPC computations with shortInts appear much slower than integer computations, especially when contrasted with Delphi. Could this be due to the byte alignment of the data? I am including source for the FPC and Delphi Console applications.
Lazarus 9.25 (SVN 12889) with FPC 2.2.1 Windows XP CoreDuo 2.16GHZ
-O1 -OG (fast optimizations ,faster code)
integer329
single781
double1219
integer312
shortint594
-O3 -OG (intense optimizations, faster code)
integer94
single781
double1235
integer94
shortint312
Delphi4 Windows XP CoreDuo 2.16GHZ
Default settings
integer47
single765
double766
integer47
shortint47
Additional information:
----------------------------------------------------------------------
FPC/LAZARUS CODE
unit TestCase1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testutils, testregistry;
type
TTestCase1= class(TTestCase)
published
procedure TestHookUp;
end;
implementation
uses
{$IFDEF Unix}
lclintf; //gettickcount
{$ELSE}
Windows; //gettickcount
{$ENDIF}
{ TForm1 }
{$A8}
procedure ReportTime (lStr: string; lStartTime: DWord);
begin
writeln(lStr+inttostr(GetTickCount-lStartTime));
end;
const kReps = 100000000;
procedure doSingle;
var
I:integer;
sum:single;
lStartTime:DWord;
begin
sum:=0;
lStartTime:=GetTickCount;
for I:=0 to kReps do
begin
sum:=sum+2;
sum:=sum-2;
end;
ReportTime('single',lStartTime);
end;
procedure doDouble;
var
I:integer;
sum:double;
lStartTime:DWord;
begin
sum:=0;
lStartTime:=GetTickCount;
for I:=0 to kReps do
begin
sum:=sum+2;
sum:=sum-2;
end;
ReportTime('double',lStartTime);
end;
procedure doint;
var
I:integer;
sum:integer;
lStartTime:DWord;
begin
sum:=0;
lStartTime:=GetTickCount;
for I:=0 to kReps do
begin
sum:=sum+2;
sum:=sum-2;
end;
ReportTime('integer',lStartTime);
end;
procedure doShort;
var
I:integer;
sum:shortint;
lStartTime:DWord;
begin
sum:=0;
lStartTime:=GetTickCount;
for I:=0 to kReps do
begin
sum:=sum+2;
sum:=sum-2;
end;
ReportTime('shortint',lStartTime);
end;
procedure TTestCase1.TestHookUp;
begin
Fail('Write your own test');
end;
initialization
RegisterTest(TTestCase1);
doInt;
doSingle;
doDouble;
doInt;
doShort;
readln;
end.
----------------------------------------------------------------------
DELPHI CODE
program d3console;
{$APPTYPE CONSOLE}
uses Windows, sysutils;
procedure ReportTime (lStr: string; lStartTime: DWord);
begin
writeln(lStr+inttostr(GetTickCount-lStartTime));
end;
const kReps = 100000000;
procedure doSingle;
var
I:integer;
sum:single;
lStartTime:DWord;
begin
sum:=0;
lStartTime:=GetTickCount;
for I:=0 to kReps do
begin
sum:=sum+2;
sum:=sum-2;
end;
ReportTime('single',lStartTime);
end;
procedure doDouble;
var
I:integer;
sum:double;
lStartTime:DWord;
begin
sum:=0;
lStartTime:=GetTickCount;
for I:=0 to kReps do
begin
sum:=sum+2;
sum:=sum-2;
end;
ReportTime('double',lStartTime);
end;
procedure doint;
var
I:integer;
sum:integer;
lStartTime:DWord;
begin
sum:=0;
lStartTime:=GetTickCount;
for I:=0 to kReps do
begin
sum:=sum+2;
sum:=sum-2;
end;
ReportTime('integer',lStartTime);
end;
procedure doShort;
var
I:integer;
sum:shortint;
lStartTime:DWord;
begin
sum:=0;
lStartTime:=GetTickCount;
for I:=0 to kReps do
begin
sum:=sum+2;
sum:=sum-2;
end;
ReportTime('shortint',lStartTime);
end;
begin
doInt;
doSingle;
doDouble;
doInt;
doShort;
readln;
end.
Mantis conversion info:
- Mantis ID: 10201
- Version: 2.2.1
- Fixed in version: 2.4.0
- Fixed in revision: 9329 (#fcb37c4f)
- Monitored by: » @neurolabusc1 (Chris Rorden)