Any better idea to store values in a program as a cache...?

In my program i calculate statistical tests between different number of variables (It may be several thousands), I am storing these values in static variable and each time i checked whether test is already calculated or not,
If it is already done then retrieve its value otherwise calculate this test and store it for next use. In my program such type of tests (which to be store) may be several lakhs (hundreds of thousands).

I declared a user defined datatype by structure

1
2
3
4
5
struct MIResultBackup
{
    bool test;
	float MIValue, pValue;
};


later i use a static map to store these values.
 
static std::map<string, MIResultBackup> slMI::testValuesCache;


I generate a string by concatenating the variable indexs to use them as a key n map.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Generate a string by variables names and conditional variables like v121234
	stringstream varNames;
	varNames <<"v";
	for (int i = 0; i < varAB.size(); i++)
	{
		varNames <<varAB[i];
	}
	for (int i = 0; i < cond_vars.size(); i++)
	{
		varNames <<cond_vars[i];
	}
	//cout<<"\n var names as a string = "<< varNames.str();

	// Find whether test already calculated.
	std::map<string, MIResultBackup>::iterator map_Iter;
		
	map_Iter = testValuesCache.find(varNames.str());

	bool test = false;
	MIResultBackup testResult; // to store the chi2 values

	if (map_Iter == testValuesCache.end())
	{
		test = mutualInformation::computeMI(a, b, cond_vars);

		testResult.test = test;
		testResult.MIValue= mutualInformation::getMI();
		testResult.pValue = mutualInformation::getPValue();
		testValuesCache.insert(make_pair(varNames.str(), testResult));

	}
	else
	{
		test = map_Iter->second.test;
		mutualInformation::MI = map_Iter->second.MIValue;
		mutualInformation::p = map_Iter->second.pValue ;
	    cout<<"*";
	
		
	}


I would like to know any better idea to store values as a cache.
Thanks in advance.
Topic archived. No new replies allowed.