# Thread: Function Called With Same Arguments Again & Again

1. ## Function Called With Same Arguments Again & Again

My algorithm uses a function like .......
insert
Code:
```long double E(int x1, int x2, int x3, int x4, int x5)
// x1..x5 start from 1 and go upto few thousand (2,500)
{
decoding of x1,x2,x3,x4,x5 takes place ......
and parmeters are chosen from the problem data set

for(int l=1; l<5; l++)
for(int i=1; i<5; i++)
{

case 1:
long doubel f1( parameters of xi )
{
long double g1( parameters of xi ) ;
long double h1( parameters of xi ) ;
................
}
case 2:
long doubel f2( parameters of xi )
{
long double g2( parameters of xi ) ;
long double h2( parameters of xi ) ;
................
}
case 3:
long doubel f1( parameters of xi )
{
long double g1( parameters of xi ) ;
long double h1( parameters of xi ) ;
................
}
}

}```
that is the function is complex and involves further calls of other funtions

A number of sets {x1,x2,...,xn} are produced during the program run for which the function E() has to be evaluated. The problem is that a set of values of {x1,x2,...,xn} may repeat itself again & again. And it is unnecessary to compute the function value for the same set of values as the value will be the same with only the additional computation time. And computation time has to be minimized for efficient programming.

How can i do this ..............

Somebody told me that hashing can be used here ..... but i don't know whether he is right or not as i have never done hashing ......... it is not known to me .....

Can anybody suggest me a simple way to avoid calling the same function for duplicate sets of values ..... in order to save computation time.?

2. I would use a hashing algorithm to determine if x1, x2, x3, x4 and x5 are the same as some previous set.

The problem is coming up with a good hash-algorithm. Are all the values in the range 0 .. 2500 used equally, or are some value(range)s more common than others?

--
Mats

3. Well, it also depends on what calculating the values involves, but you could keep a record for each set of the calculated value adn a flag as to whether it has been previously computed. This is how I woudl do it -

Code:
```
class MySet {
public:
MySet(int members);
~MySet();

BOOL Calculated;
double ReturnValue;

double* TheData;
} ASet;

MySet::MySet(int members){
this->Calculated = FALSE;
this->Members = members;
this->TheData = (double*)malloc(members * sizeof(double));
return;
}

MySet::~MySet(){
if(this->TheData != NULL) free(this->TheData);
return;
}

void main(void){ // kisses sweety :)
double Foo;

MySet* Data = new MySet(10);

Foo = SomeFunc(Data);
return;
}

double SomeFunc(MySet* pSet){
if(pSet->Calculated) return pSet->ReturnValue;

// actually do the calculation

pSet->Calculated = TRUE;

return pSet->ReturnValue;
}```

4. But you would need a set of 2500 ^ 5. With 11102 TB, it may be a little bit out of reach for most of us.

--
Mats

5. Have you looked into using C++ Maps? Here's a reference link http://www.cppreference.com/cppmap/index.html.

Use the x as the key and use the result of your computation as the value if you need to.

HTH

6. Originally Posted by pantherse
Have you looked into using C++ Maps? Here's a reference link http://www.cppreference.com/cppmap/index.html.

Use the x as the key and use the result of your computation as the value if you need to.

HTH
You would of course have to take into account that there are 5 values of x that need to be combined somehow. My idea would be to generate a string of 20 characters that is filled with the values of the x1..x5 [with space or zero-padding to make each elemen the same length].

But that still assumes that there's a great sparseness in the values used - if all values for all positions are used, then the table would be ginormous, and not work out very well.

If there is a huge number of actively used options, then using some sort of cache to hold the last X number of values, and if we find a repeat, then use the cached value, if not, then just add the latest value to the cache (and if the cache is full, remove some other cache-entry).

--
Mats