Efficient Programming: Saving System Resources in Your Program

Written by Raza on. Posted in C++

Programming is not a Big Deal. The Big Deal is Efficient Programming. Efficient programming is a very comprehensive term which includes writing a code which uses as less system resources as possible, is free from most of the errors  and is capable of handling all kinds of situations. I shall show you first a piece of code and then explain to you some of these aspects for writing an efficient code. Kindly follow the following piece of code. 

</p>
<p>string copyData (vector&lt;char&gt; v1, string str)<br />
{<br />
.  string temp ;<br />
.  for (int i = 0 ; i &lt; v1.size() ; i )<br />
.   temp.push_back(v1[i]) ;</p>
<p>.</p>
<p>.  for (int j = 0 ; j &lt; str.size() ; j )</p>
<p>.   temp.push_back(str[j]) ;<br />
.  return temp ;<br />
}</p>
<p>

<br />
int main ()<br />
{<br />
.  vector&lt;char&gt; v1(10, "a") ;<br />
.  string str = "hello world" ;<br />
.  cout &lt;&lt; copyData(v1, str) ;<br />
}</p>
<p>

This code adds up the content of both the data members into one string. I hope you are familiar with string and vector object with their built in functions. If not, please look them up somewhere. See the list of online resources to practice C . The code runs perfectly good, so what is to be done more to it? Well, with this sort of code, you surely won’t be able to secure yourself a good job. Good and efficient programming is essential to establishing yourself in software industry. There are many issues with this code regarding memory and system resources. I shall explain them to you one by one.

.

1. Use const in the parameters:

You pass to objects of vector and string type as arguments to the function. As we have already learnt when we wrote copy constructor, whenever you pass an object by value, copy constructor gets called. A duplicate of your object gets created inside that function. In this case, many extra bytes of memory are used just because you insist on passing your objects by value. Remember! Always pass your objects to a function mobile casino by reference. If you fear any change in their values, make then constant by preceding them with const. That would save a lot of your system memory and resources and is considered efficient programming technique.

.

2. Returning by Value:

You create a temporary string and then return it by value. First avoid returning by value to avoid calling copy constructor. But here, can you pass the string by reference. Surely Not! It would create havoc in your program to return the reference of a local variable. After the execution of the function, that variable would get destroyed and you would be left with a dangling reference. This is very much like having address of a house that doesn”t exist or holding the check of a bank that is bankrupt. It brings down the whole building of efficient programming.

For efficient programming, it is better if you pass this third string into the function arguments. Definitely you would want to use this string otherwise you would not have made this function in the first place. But you might argue that by creating this temp string in main, it would stay as long as the whole program is running; another problem. So it depends upon the circumstances. If you can create the string where you call the function and pass it as an argument, you are better off. But if execution time of your program is great, you can afford to return it by value. Here I would pass it to the function anyways.

.
3. Post Increment Operator:

Inside the function, you are running two for loops. The loop variable is ‘i’ and it is being incremented each time the loop operation is performed. Now please don’t laugh it off but this ‘i ’ is not correct way to do it. I haven’t covered it yet but when you would learn to overload post increment and pre increment operators, you would appreciate this fact. When you do ‘i ’, compiler calls a function for adding one to this variable. That function creates a temporary variable, copies the original variable into it, increments the original variable and returns the temp variable. So EXTRA system resources are being consumed. If your program is quite large, you would be using hundreds maybe thousands of bytes this way which drastically reduces program efficiency. Got it?

In this program, you are calling the ‘i ’ 10 times and ‘j ’ 12 times. An integer takes 4 bytes each. It means a total of 88 extra, useless memory is being consumed in your just one function. Imagine you call this function 10 times in your program, resulting in about one kilobyte extra memory and system resources being used. The solution is; use pre increment instead of post increment. Use ‘ i’ instead of ‘i ’. It is a very small maybe insignificant or useless but highly efficient programming practice.

Here is the improved version of that code.

</p>
<p>void copyData (const vector&lt;char&gt; &amp;v1, const string &amp;str, string &amp;temp)<br />
{<br />
.  for (int i = 0 ; i &lt; v1.size() ; i )<br />
.   temp.push_back(v1[i]) ;<br />
.  for (int j = 0 ; j &lt; str.size() ; j )<br />
.   temp.push_back(str[j]) ;<br />
}<br />
int main ()<br />
{<br />
.  vector&lt;char&gt; v1(10, "a") ;<br />
.  string str = "hello world" ;<br />
.  string temp ;<br />
.  copyData(v1, str, temp) ;<br />
.  cout &lt;&lt; temp ;<br />
}</p>
<p>

I’ve found these much errors in there. If you can find more, let me know in the comments.

Tags: , , , , ,