Problem 908

Summary: G4IonTable::IsLightIon is not optimized
Product: Geant4 Reporter: vincenzo.innocente
Component: particlesAssignee: kurasige
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 8.1   
Hardware: PC   
OS: Linux   

Description vincenzo.innocente 2006-11-11 02:47:23 CET
In some simple examples run on slc3 G4IonTable::IsLightIon ends up eating most of the cpu due to too
many string creation
Although more modern compilers may mitigate the problem I wish to suggest to modify the code
as follows (I think that for all practical use cases a linear search is better than a binary one)

#include <algorithm>
G4bool G4IonTable::IsLightIon(G4ParticleDefinition* particle) const
{
  static const std::string names[] = { "proton", "neutron", "alpha", "deuteron",
                           "triton", "He3", "GenericIon"};

   // return true if the particle is pre-defined ion
  return std::find(names, names+7, particle->GetParticleName())!=names+7;
}
Comment 1 Gabriele Cosmo 2006-11-16 01:28:59 CET
Thanks. The fix will be included in release 8.2.