What type of variable allocates less memory, integer or string?

Asked

Viewed 1,154 times

2

Imagining the situation I’m taking from the database a field number, which represents the client’s code.

This value tends to be between 1 to 50000. I must for this case save as string or how integer to allocate less memory?

Examples:

Mode 1:

procedure TFrmCadCliente.Teste(Sender: TObject);
var
  vCodCliente : integer;
begin
  vCodCliente := cdsClienteCodCliente.AsInteger;
end;

Mode 2:

procedure TFrmCadCliente.Teste(Sender: TObject);
var
  vCodCliente : string;
begin
  vCodCliente := cdsClienteCodCliente.AsString;
end;

I know that if I use the code as an integer I should then convert and in this case it is better to save directly as integer, but for my case in particular this will not be necessary.

2 answers

12


What kind of fruit fits more units from inside a box of same size, grape or watermelon? Even if you know what it is, what’s the point if you like watermelon?

You should use numeric types to represent numbers, something that depends on calculations, that represents quantities of something (date, stock value, price, etc.). And you should use text to represent descriptions, even if these descriptions have only numeric characters (phone, CPF, etc.).

What you are talking about is something countable (do not need to count, just be a quantity) and should be integer.

But if you really want to know which takes up the most space is the string. A guy integer will take 4 bytes and is per value, so the value is already in the object itself. A string is a type by reference, so it will already take 4 or 8 bytes (if it is 32 or 64 bytes) only to represent the pointer to the real object. And then the real object will occupy at least 4 bytes (in fact it is almost certain that it is much more, I do not know the implementation details of Delphi for this type).

There will be 4 bytes because I know this type has its lifetime controlled by a reference counter that needs at least 4 bytes, maybe 8 bytes. I’m considering that a string empty has some optimization, but is unlikely. It probably has another 1 to 8 bytes (always depending on the architecture that is running and the type string specific) to indicate the size of this string (in fact it may be that limit to 4 per decision of the implementers). And there may be some other metadata, like a pointer to the type to indicate polymorphism, but maybe you don’t need it in string because it is a class that cannot be inherited, but it inherits, so you may need to.

And then it will have at least 1 byte more for each character, in this case if it is the text 50000, will be 5 bytes in standard implementations.

As I don’t know the actual implementation of Delphi it may be that it still has one more terminator character (\0). And it may be more than 1 byte per character, depending on the encoding (I know Delphi has several , but I don’t know what the default is). And there may be optimizations to store the size of the string.

All this in Delphi, already in the database is different, but the basic idea is the same, the text has 1 byte (or 2 or 4) per character and the integer has 4 bytes (depends a little on the technology).

Anyway, integer is much smaller and simpler, and gives less problem, is more performatic for several reasons.

  • Thank you very much, that’s exactly what you put that I wanted to understand.

5

In Delphi there are several integer types, each occupying a given amount of memory and with a given range of possible values. inserir a descrição da imagem aqui

In your example could use a WORD type, which would occupy only 16bits of memory and has the capacity to store values from 0 to 65535.

Already the string types occupy variable space in memory, depending on the size of the string inserir a descrição da imagem aqui

Logo using strings the memory size is variable according to the number size.

Now in a matter of good programming practice, if the value is a number, it should be stored as a number, the code will be more readable and easy to do future maintenance, avoiding several unnecessary conversions that will only increase the processing of your application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.