Programming Trick #1 - Increase Code efficiency via Reduction in Variable declaration


Hello guys, 
Let me give you my short bio first of all. I am M.s. Saggoo , a young geek and new member at LeDotnet team.
Its my pleasure to tell you that, we will also have posts in English from now.
Programming Trick #1 - Increase Code efficiency via Reduction in Variable declaration


A variable takes memory depending on its datatype. Example character takes up 1 byte of memory, integer takes 4 bytes of memory and similarly all other datatypes.








Common use of integer declaration is like following:
Variable declaration -- VERY HIGH means More memory
Instead of declaraing too many variables on one go, try to implicitly convert the incoming data into datatype you want. This data will be the one which user feeds during runtime of application.
Replace c=a+b 
with 
Console.WriteLine(Convert.ToInt32(Console.ReadLine()) + Convert.ToInt32(Console.ReadLine()));

No integer declared, Less memory


In above code not even a single integer is declared. It works perfectly. The former one was more time consuming because system had to allocate memory for integers and assign them values. But later one is less time consuming and has more efficiency because it does not use standard space of 4+4+4 =12 bytes of memory which former code takes.

Ask questions, if you have any, in comments..

2 Comments

Previous Post Next Post