3

This question is related to the physical memory of a C# Program. As we know that, byte variable consumes 1 byte of memory and on the other hand an int (32-bit) variable consumes 4-bytes of memory. So, when we need variables with possibly smaller values (such as a counter variable i to iterate a loop 100 times) which one should we use in the below for loop? byte or int ?

 for(byte i=0; i<100; ++i)

Kindly please give your opinion with reason and share you precious knowledge. I shall be glad and thankful to you :-)

Note: I use byte instead of int in such cases. But I have seen that many experienced programmers use int even when the expected values are less than 255. Please let me know if I am wrong. :-)

HN Learner
  • 544
  • 7
  • 19
  • 1
    In my opinion, whenever I do loop, I generally don't know how large is the array size (for now and the future) so it is safer to use int. For example, now the array size is 255, tomorrow it can become 500. So you will need to change the code and redeploy. – Ray Krungkaew Apr 01 '17 at 14:26
  • These may help you http://stackoverflow.com/questions/1018234/performance-of-byte-vs-int-in-net http://stackoverflow.com/questions/2346394/should-i-use-byte-or-int – Anıl Aşık Apr 01 '17 at 14:36
  • Thanks For the Response ... :-) – HN Learner Apr 01 '17 at 14:40
  • 1
    you can check this also http://stackoverflow.com/questions/1097467/why-should-i-use-int-instead-of-a-byte-or-short-in-c-sharp – Usman Apr 01 '17 at 14:49
  • Thanks All :-) ... I got my answer ! – HN Learner Apr 01 '17 at 14:52
  • 1
    While this question was asked enough times already and have good number of duplicates it is rare to see such claim as "As we know that, byte variable consumes 1 byte of memory" - I'd recommend back that statement by some authoritative source or least look and measure yourself. Clearly sample provided in the post will consume more than one byte to provide space for byte variable (likely at least 4 bytes). Also make sure to read [horses by Eric Lippert](https://ericlippert.com/2012/12/17/performance-rant/) to improve you future perf questions. – Alexei Levenkov Apr 01 '17 at 14:54
  • Okay @AlexeiLevenkov .. i am Sorry . I realized that my statement was wrong . Thanks for pointing out ! – HN Learner Apr 01 '17 at 16:58

1 Answers1

4

In most cases, you won't get any benefit from using byte instead of int. The reason is: If the loop variable is stored in a CPU register: Since modern CPUs have a register width of 32 bits, and since you can't use only one fourth of a register, the resulting code would be pretty much the same either way.

If the loop variable is not stored in a CPU register, then it will most likely be stored on the stack. Compilers try to align memory locations at addresses which are multiples of 4, this has to do with performance, thus the compiler would also assign 4 bytes to your byte variable on the stack.

Depending on details of your code, the compiler would even add extra code to make sure that the memory location (on the stack or in a register) never exceeds 255, which would add extra code and makes it slower. It's a totally different story with 8 bit microcontrollers like those from Atmel and Microchip, there your approach would make sense.

Heinz Kessler
  • 1,610
  • 11
  • 24
  • Thanks for sharing Sir :-) But if byte data type consumes equal memory as that of int.. then what is the purpose of byte data type in C# ? Actually I'm confused about the point that, "When should we use the byte data type"? – HN Learner Apr 01 '17 at 14:44
  • 1
    Side note: Definition of "modern" used in the post seem to be a bit strange... I have no idea where 32-bit CPUs are "modern" in 2017. – Alexei Levenkov Apr 01 '17 at 14:48
  • 1
    Byte type is essentially used when doing interop, p/invoke where you need to put the exact type to make it work, or when manipulating Streams (File/Network/...) – Arnaud F. Apr 01 '17 at 14:55
  • 1
    @Alexej Levenkov: Then let's call it: Modern applications, since most PC applications are 32 bit nowadays, not 64 bit. – Heinz Kessler Apr 01 '17 at 15:48
  • 1
    @HN Learner: They byte type is used in cases where you only need 8 bits of storage and you don't want to waste 3 bytes of memory space per variable, e.g. if you have very large arrays in memory. You can also save space in structs by using the StructLayoutAttribute attribute if you have large amounts of instances of this structure. But for a single variable, it's not worth to use byte. – Heinz Kessler Apr 01 '17 at 15:56
  • Okay Got It Thanks @HeinzKessler .. Got It :-) – HN Learner Apr 01 '17 at 16:55