Forum > General

which is better byte or integer ??

(1/5) > >>

majid.ebru:
Hi

maximun number of my var in my function  is 20?

i use Byte or Integer ?

which is better byte or integer??


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var     i: Byte; // Max i = 20...var     i: Integer; // Max i = 20 

Handoko:
I can be wrong, but if I remember correctly some (modern) processors are optimized to use integer and Int64. But you usually won't get any noticeable performance different, except you're writing some CPU intensive calculations and use it in a massive looping.

If only to store value from 0 .. 20, I personally will use Byte. But for compatibility reason, I may use integer. Or if I think someday in the future I may use the code and it may grow bigger, for example for Index or Count, I will use integer.

J-G:

--- Quote from: Handoko on August 15, 2017, 06:00:38 pm ---If only to store value from 0 .. 20, I personally will use Byte. But for compatibility reason, I may use integer. Or if I think someday in the future I may use the code and it may grow bigger, for example for Index or Count, I will use integer.
--- End quote ---
This is still an interesting question - even after 12 month of using FPC/Lazarus :)

I made a decision soon after finding FPC/Laz and seeing that 'Integer' can be either 2 or 4 bytes.  I never use 'Integer'  -  where a value (+ and not real) is always going to be <=255, I use byte, <= 65535, Word  etc.

Handoko:
Integer can be 2 or 4 bytes because it depends on the hardware it used when compiling the program.

Using Integer has one advantage which I know for sure. Large data type is considered expensive (and slow) especially on the old days. If for example 30 years ago I wrote a data module using Longint for RecNo or RecordCount, it might considered as not efficient because we would less likely to use such large data in the past. But If I now reuse the code which used Integer (from 30 years in the past), now it will automatically mapped to to Longint. It is reasonable because now harddisk is cheaper, data and files are bigger. If you want the data type size for sure, use SmallInt or LongInt instead of Integer.

So Integer has flexible size and it is designed for that purpose (I think). Maybe 10 years latter it will be defined to has Int64 size. Using Integer can mean, that code is designed to survive in the future.

I understand the reason not use Int, SmallInt to store only positive values. I often use Byte and Word too. But for some cases I use Integer for them. For example for Age. We know no one has age < 0. But if I use Integer, I can use -1 to indicate that value has not been provided, or nil or not valid.

wp:
Unsigned integers (byte, word, cardinal) require tight attention as loop variables. Imagine what will happen if you use them in a plain old "for" loop when the upper limit is 0. How often will the loop execute?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$mode objfpc}{$H+} procedure something;begin  WriteLn('something');end; var  i: Word;  count: Word;begin  Count := 0;  for i:= 0 to Count-1 do    something();end.

Navigation

[0] Message Index

[#] Next page

Go to full version