Byte vs Byte Array in Memory
The code below uses 1 byte of memory.
byte[] n = new byte[] { 10, 20, 30, 40 }; //memory looks like this 10 | 20
| 30 | 40
vs this code below which uses 4 bytes of memory.
byte n1 = 10; // memory looks like this | 10 | 0 | 0 | 0 | 20 | 0 | 0 | 0
| 30 | 0 | 0.....
byte n2 = 20;
byte n3 = 30;
byte n4 = 40;
This is tested in Visual Studio 2012 + 2010, I thought modern compilers
were supposed to do optimization for me? If it truly is faster to put
spacing so that the byte secretly becomes a non-functional int then how
come it doesn't do the same for arrays and would a byte array thus become
faster if it occupied 4 bytes per value thus rendering the whole purpose
of byte completely useless on a 32 bit / 64 bit machine?
No comments:
Post a Comment