X

Tips and Programming Techniques

An important rule to make an article attractive to programmers is to have some trick or technique that can elevate our software over others, especially insert the word “How to …” explaining the procedure to be used. What then is the true value of a trick, and the costs to implement it. Best programming techniques in fact descended from assumptions that reside in the depths of an operating system or hardware of a computer.
A programmer who has several years of experience clearly remembers that it was necessary to optimize the code, especially that involving the use of pointer arithmetic and array instead of indexes. The fact compiler must perform the horrible multiplications to access an element of an array through indexes, operation much faster through the increase of pointers. Another technique to speed up the code is to use the pre-increment in the amounts rather than post-increment, this is because in the latter case the compiler should return a value not yet increased (a phenomenon that can be seen well when used overloading operators sum). As an example we see a program that runs two vectors and places the result of the sum of the elements in a third vector.

#include <iostream>
using namespace std;

void Sum_Array(int* pia, int* pib, int* pic)
{
 for (int i = 0; i < 5; i++)
  pic[i] = pia[i] + pib[i];
}

void main (void)
{
 int a[] = {5, 12, 21, 54, 12};
 int b[] = {7, 9, 6, 21, 15};
 int c[5];

 Sum_Array(a, b, c);

 cout << "Initial array a: ";
 for (int i = 0; i < 5; i++)
  cout << a[i] << ", ";
 cout << endl << endl;

 cout << "Initial array b: ";
 for (int i = 0; i < 5; i++)
  cout << b[i] << ", ";
 cout << endl << endl;

 cout << "Result array: ";
 for (int i = 0; i < 5; i++)
  cout << c[i] << ", ";
 cout << endl << endl;

 system("pause");
 }

This method is less powerful than the use of pointers.

#include <iostream>
using namespace std;

void Sum_Array(int* pia, int* pib, int* pic)
{
 for (int i = 0; i < 5; i++)
  *(pic++) = *(pia++) + *(pib++);
}

void main (void)
{
 int a[] = {5, 12, 21, 54, 12};
 int b[] = {7, 9, 6, 21, 15};
 int c[5];

 Sum_Array(a, b, c);

 cout << "Initial array a: ";
 for (int i = 0; i < 5; i++)
  cout << a[i] << ", ";
 cout << endl << endl;

 cout << "Initial array b: ";
 for (int i = 0; i < 5; i++)
  cout << b[i] << ", ";
 cout << endl << endl;

 cout << "Result array: ";
 for (int i = 0; i < 5; i++)
  cout << c[i] << ", ";
 cout << endl << endl;

 system("pause");
 }

And better yet the use of pointers with the pre-increment.

#include <iostream>
using namespace std;

void Sum_Array(int* pia, int* pib, int* pic)
{
 --pia;
 --pib;
 --pic;

 for (int i = 0; i < 5; ++i)
  *(++pic) = *(++pia) + *(++pib);
}

void main (void)
{
 int a[] = {5, 12, 21, 54, 12};
 int b[] = {7, 9, 6, 21, 15};
 int c[5];

 Sum_Array(a, b, c);

 cout << "Initial array a: ";
 for (int i = 0; i < 5; ++i)
  cout << a[i] << ", ";
 cout << endl << endl;

 cout << "Initial array b: ";
 for (int i = 0; i < 5; ++i)
  cout << b[i] << ", ";
 cout << endl << endl;

 cout << "Result array: ";
 for (int i = 0; i < 5; ++i)
  cout << c[i] << ", ";
 cout << endl << endl;

 system("pause");
 }

From the code we can see that we had to decrease the initial pointers to use them in a loop, to be observed that in some compilers this initial process could cause an exception. The use of code optimization with tricks and special techniques was useful on the first compilers of the seventies, in one of those modern version is faster than the first, although it uses indexes. This phenomenon occurs because modern compilers optimize the code by yourself at the time of compilation of the source and also does not give rise to exceptions. Basically my advice is to not use clever tricks and techniques homemade because they are now compilers that do this for us automatically and much better.

Categories: Programming
giampy107:
X

Cookies Policy

We use cookies to personalize content and ads, provide social media features and analyze our traffic. We also share information about your use of our site with our web analytics, advertising and social media partners who may combine it with other information you have provided to them or that they have collected based on your use of theirs. services.

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings