Recent

Author Topic: [Answered]Which is faster? DataSet.Fields[Index].AsString or TField.AsString?  (Read 13496 times)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Quote
I've found this link http://www.freepascal.org/docs-html/fcl/db/tfieldtype.html but no summary of  T<fieldtype>Field.
On that page click on [index] and you get all the T<fieldtype>Fields under the letter T  (http://www.freepascal.org/docs-html/fcl/db/index-8.html)

Knipfty

  • Full Member
  • ***
  • Posts: 230
OK.  I've looked over db.pas.  Very interesting.  I've found this mapping:
Code: [Select]
const
  DefaultFieldClasses : Array [TFieldType] of TFieldClass =
    ( { ftUnknown} Tfield,
      { ftString} TStringField,
      { ftSmallint} TSmallIntField,
      { ftInteger} TLongintField,
      { ftWord} TWordField,
      { ftBoolean} TBooleanField,
      { ftFloat} TFloatField,
      { ftCurrency} TCurrencyField,
      { ftBCD} TBCDField,
      { ftDate} TDateField,
      { ftTime} TTimeField,
      { ftDateTime} TDateTimeField,
      { ftBytes} TBytesField,
      { ftVarBytes} TVarBytesField,
      { ftAutoInc} TAutoIncField,
      { ftBlob} TBlobField,
      { ftMemo} TMemoField,
      { ftGraphic} TGraphicField,
      { ftFmtMemo} TBlobField,
      { ftParadoxOle} TBlobField,
      { ftDBaseOle} TBlobField,
      { ftTypedBinary} TBlobField,
      { ftCursor} Nil,
      { ftFixedChar} TStringField,
      { ftWideString} TWideStringField,
      { ftLargeint} TLargeIntField,
      { ftADT} Nil,
      { ftArray} Nil,
      { ftReference} Nil,
      { ftDataSet} Nil,
      { ftOraBlob} TBlobField,
      { ftOraClob} TMemoField,
      { ftVariant} TVariantField,
      { ftInterface} Nil,
      { ftIDispatch} Nil,
      { ftGuid} TGuidField,
      { ftTimeStamp} Nil,
      { ftFMTBcd} TFMTBCDField,
      { ftFixedWideString} TWideStringField,
      { ftWideMemo} TWideMemoField
    );

What I also see is that even if you define a field as either TField or TStringField, they look like they are equivalent as far as these calls go, TField.AsString / TStringField.Value or TStringField.AsString.  In that they all call the property "function GetAsString: string; override;"

So assuming that you are not using anything special in the T<fieldtype>Field, like extra properties or methods, then it really doesn't make any difference, except perhaps your code is a little easier to read.
32-bit Lazarus 1.0 FPC 2.6.0, 64-bit Win 7, Advantage DB 10.10

taazz

  • Hero Member
  • *****
  • Posts: 5365
OK.  I've looked over db.pas.  Very interesting.  I've found this mapping:

That's the default mapping,  If I remember correctly you are using advandage component set I would suggest to take a look on those component's GetFieldClass method. This will give you the correct mapping for your components.
 
What I also see is that even if you define a field as either TField or TStringField, they look like they are equivalent as far as these calls go, TField.AsString / TStringField.Value or TStringField.AsString.  In that they all call the property "function GetAsString: string; override;"

That is by design if you take a closer look to other field types you will see the same pattern eg TIntegerField.Value uses the Set/GetAsLongint methods. It seems they have been designed with speed in mind.

So assuming that you are not using anything special in the T<fieldtype>Field, like extra properties or methods, then it really doesn't make any difference, except perhaps your code is a little easier to read.

True, if you use the type specific fields there is no difference if you use .Value or .As<NaturalType> methods.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

KpjComp

  • Hero Member
  • *****
  • Posts: 680
@Knipfty, Yes,

But TField.Value <> TStringField.Value.

The main advantage to using the correct TField type is that you get proper type checking at compile time.   So at compile time you couldn't say assign a TStringField to an an integer, if you use the .Value, but if you use the .AsInteger it will compile, but potentially give runtime errors.  So if you stick to .Value and T(Type)Field then you have full type checking.

Knipfty

  • Full Member
  • ***
  • Posts: 230
Yep, tweaked my code a little bit to use TStringField instead of TField.

I'm glad I asked my original question which then lead to this result.

Great thread everyone.  Thanks!

Knipfty
32-bit Lazarus 1.0 FPC 2.6.0, 64-bit Win 7, Advantage DB 10.10

 

TinyPortal © 2005-2018