Memoization
Take a look at the Wikipedia entry on Memoization for the nitty gritty.
Esentially, it's a term for a specific form of caching, or so it would appear. Take the following data-set:
If you wanted to work out how many people were in each family, you could:
Esentially, it's a term for a specific form of caching, or so it would appear. Take the following data-set:
List Families [FamilyId : Name] {1 : Smith, 2 : Jones}
List People [PersonId : Name] {1: Adam, 2 : Ben, 3 : Carol, 4 : David}
List PeopleInFamilies [FamilyId : PersonId] { 1 : 1, 1 : 2, 1 : 3, 2 : 4]
If you wanted to work out how many people were in each family, you could:
foreach(familyItem in Familes)However, for a set of 1000 families and 800 people in families, this would mean that there would be 800,000 iterations over the "PeopleInFamilies" dataset. The code could be re-written more sensibly as:
{
counter = numberOfPeopleInFamily(familyItem.FamilyId)
print("FamilyId {0} Count {1}", familyItem.FamilyId, counter);
}
function numberofPeopleInFamily(familyId)
{
counterInFunction = 0;
foreach(peopleInFamiliesiitem in PeopleInFamiles)
{
if familyId = peopleInFamiliesiitem.FamilyId
{
counterInFunction++;
}
}
return counterInFunction;
}
dictionary<int, int> familyIdByCountInFamily = numberofPeopleInFamilies;Now there are only 800 iterations over the "peopleInFamilies" dataset, much better! That's my take on memoization, anyway.
foreach(familyItem in Familes)
{
print("FamilyId {0} Count {1}", familyItem.FamilyId, familyIdByCountInFamily[familyItem.FamilyId]);
}
function numberofPeopleInFamilies()
{
dictionary<int, int> familyIdByCountInFamily
foreach(peopleInFamiliesiitem in PeopleInFamiles)
{
familyIdByCountInFamily[peopleInFamiliesiitem]++;
}
return familyIdByCountInFamily;
}

Leave a comment