-2

The profiling has pointed to this function as a performance degrader, as the recursion dives quite deep

Func(unsigned eff_id)
{



  if (eff_id == 0) return 1;

  if (eff_id == 1) return 0;

 XCodeRuleNode rn(m_IH_rn_ri.get_key(eff_id));   // Initialize 
  {
  rn.t_id = Func(rn.t_id);
  rn.f_id = Func(rn.f_id);
  //
  }

  return RegCodeRuleNode(rn);   // Inserting the object in a hash table
}
sameer karjatkar
  • 2,017
  • 4
  • 23
  • 43

1 Answers1

4

Just to answer your question: Yes, it can be converted (see also Can every recursion be converted into iteration?).

However: The recursion in itself (if any, there is no explicit recursion in the example) is probably not the degrader, but rather the depth of recursion, or in "looping-terms" the number of iterations, so just replacing recursion by iteration will probably not resolve your problem and you will probably have to look for other solutions (e.g. memoizing, look up tables, using other formulas or algorithms, maybe even multithreading; there are too many possible optimizations to name them all here).

On a sidenote: Your comments seem out of date, in return RegCodeRuleNode(rn); // Inserting the object in a hash table, I don't see anything being inserted

Community
  • 1
  • 1
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • It's hard to tell, since we don't know for sure what the questioner's code does, but it might be quite close to memoizing already. The recursive calls update a *copy* of an object that appears to have come out of some kind of structure `m_IH_rn_ri`. Why not update the object in the structure, so that you only have to do it once in the whole program for each value of `eff_id`? – Steve Jessop Nov 27 '12 at 12:35
  • I just realise I used "probably" three times in one phrase, d'oh. – Sebastian Mach Nov 27 '12 at 12:38