For any code you write, you should always take some time to think through and pick the right algorithm to use for your specific scenario.

## Introduction

For any code you write, you should always take some time to think through and pick the right algorithm to use for your specific scenario.

The problem we are going to analyze for this example is to find a maximum value of the function in a two dimensional segment.

We’ll consider only whole numbers

First we’ll write the program without consider performance. Then, we’ll discuss few ways to boost the performance of this program.

Our Scenario: We have interval for x [-100…100] and interval for y [-100…100]. Now in these two intervals we are looking for a maximum of the function (x*x + y*y)/(y*y + b).

This is a function of two variables: x and y. There is one more constant which could be different and user will enter it. This constant b is always greater than 0 and also lesser than 1000.

In our program ,we will not use function pow() that is implemented in math.h library. It would be interesting exercise to figure out which approach would create faster code.

Example code:

#include <iostream> #define LEFT_MARGINE_FOR_X -100.0 #define RIGHT_MARGINE_FOR_X 100.0 #define LEFT_MARGINE_FOR_Y -100.0 #define RIGHT_MARGINE_FOR_Y 100.0 using namespace std; int main(void) { //Get the constant value cout<<"Enter the constant value b>0"<<endl; cout<<"b->"; double dB; cin>>dB; if(dB<=0) return EXIT_FAILURE; if(dB>1000) return EXIT_FAILURE; //This is the potential maximum value of the function //and all other values could be bigger or smaller double dMaximumValue = (LEFT_MARGINE_FOR_X*LEFT_MARGINE_FOR_X+LEFT_MARGINE_FOR_Y*LEFT_MARGINE_FOR_Y)/ (LEFT_MARGINE_FOR_Y*LEFT_MARGINE_FOR_Y+dB); double dMaximumX = LEFT_MARGINE_FOR_X; double dMaximumY = LEFT_MARGINE_FOR_Y; for(double dX=LEFT_MARGINE_FOR_X; dX<=RIGHT_MARGINE_FOR_X; dX+=1.0) for(double dY=LEFT_MARGINE_FOR_Y; dY<=RIGHT_MARGINE_FOR_Y; dY+=1.0) if( dMaximumValue<((dX*dX+dY*dY)/(dY*dY+dB))) { dMaximumValue=((dX*dX+dY*dY)/(dY*dY+dB)); dMaximumX=dX; dMaximumY=dY; } cout<<"Maximum value of the function is="<< dMaximumValue<<endl; cout<<endl<<endl; cout<<"Value for x="<<dMaximumX<<endl <<"Value for y="<<dMaximumY<<endl; return EXIT_SUCCESS; }

Now, if we analyze the code more carefully, we notice that the part for dX*dX is calculated more times than it should, in this case it is calculated 200 times and this is a was of CPU time. What we could do?

One of the trick is to create one variable dX_Squer = dX*dX, and calculate after first for repetition, then we could use that in all calculations afterwards. You just need to add one more brackets.

There are few more optimizations you can do in the above code, just try to spot them.

The next point we could consider is how general our algorithm is, versus how optimal is it from speed point of view.

In that case we could apply few algorithms depending on the size of input set. What do we mean by that?

For example, in one our earlier c++ articles, we discussed about binary numbersthat have only two ones in many zeros.

We could use MVA algorithm that could outperform the original algorithm from speed point of view on smaller numbers, the ones that are fit for unsigned long long int, but if you use my algorithm combined with vectors it could be used in some problems where you try to pick two objects that are in set.

So, in order to create the best possible solution you could merge two algorithms and apply one according to the size of the problem. So, if the number used is smaller than unsigned long long int, you could use first algorithm and if number will not fit already mentioned type of data you could use vectors or some other data structures.

Similar to this would be addition of numbers, where it is simple to consider case of long long int, but in case we need to add to big numbers that are in size way bigger than unsigned long long int you could use vectors to store them and apply operation of addition with your algorithm. If you prefer classes you could use them to, but if you don’t need OOP approach you could just use double linked list or arrays or some other more appropriate data structure.