FreePascal Information Logo Friend of FreePascal Compiler Title
Articles with Feedback, FPC News Library, PDF Collection, Mail Lists, Books, Newsgroups, IRC Open online discussion areas Research and Tutorials Tools, Compilers and Utilities Blurbs about us, advertising, etc.
Welcome to the FoFPC Research Notes: "SizeOf() vs Constants"

SizeOf() vs Constants

by: G.E. Ozz Nixon Jr.
Published: August 2009
©opyright 2009 by Friends of FPC



      While working interpreters (PaxCompiler and DECLAN), I realized we use SizeOf(type) very often. Recently, while migrating to other compilers and operating systems, we had to address FPC's Integer 16bit vs FPC's Integer 32bit. So, I had to touch all of the code for LongInt to enforce 32bit for consistancy. Anyway, I became curious if all of these SizeOf() calls were slower that having predefined constants. In short, no... here is my test code:
    Download Source IconDownload sizeof.pas source
Uses
   dxutil_environment;// contains TimeCounter for Windows, Linux and Mac

const
   LongWordSize = SizeOf(LongWord);
   LongWordSizeX2 = SizeOf(LongWord)*2;

Var
   Loop:LongWord;
   StartTime:Comp;
   X:Longint;

Begin
   StartTime:=Trunc(TimeCounter);
   For Loop:=1 to 100000000 do X:=SizeOf(LongWord);
   System.Writeln(Trunc(Trunc(TimeCounter)-StartTime));
   StartTime:=Trunc(TimeCounter);
   For Loop:=1 to 100000000 do X:=LongWordSize;
   System.Writeln(Trunc(Trunc(TimeCounter)-StartTime));
   StartTime:=Trunc(TimeCounter);
   For Loop:=1 to 100000000 do X:=SizeOf(LongWord)*2;
   System.Writeln(Trunc(Trunc(TimeCounter)-StartTime));
   StartTime:=Trunc(TimeCounter);
   For Loop:=1 to 100000000 do X:=LongWordSizeX2;
   System.Writeln(Trunc(Trunc(TimeCounter)-StartTime));
end.

    Oddly, the SizeOf() code is actually a millisecond or two faster (over 100 million calls).

G.E. Ozz Nixon Jr.
 Links and Products we find useful



ButtonGenerator.com
Valid XHTML 1.0 Transitional Internet Map
Programmer's Heaven
grat-i-fi-ca-tion - noun
the state of being gratified; great satisfaction.


"If you have a testimonial you would like to share, feel free to email me directly at ozznixon (at) (gmail.com)"

Your Web Master
Friends-Of-FPC.org
Locations of visitors to this page world map hits counter
Copyright 2009 by 3F, LLC. All rights reserved. Worldwide.
Your request was processed by server #2 in 0.002077 secs.

sponsor
This sponsor helps us with our documentation