J
Jules Winfield
I'm often in a situation where I have a Hashtable full of objects. I'm
then presented with an object and *if* that object exists in the Hashtable,
I need to remove it from the table. I therefore have a lot of code that
looks like this:
if(ht.Contains(obj)){
ht.Remove(obj);
}
It strikes me that this code is somewhat inefficient because the
Hasthtable has to be searched *twice* : once for the Contains() call and
once for the Remove() call. So I thought maybe I should approach the
situation this way:
try{
ht.Remove(obj);
}
catch(Exception){}
Now I'm only searching the Hashtable once... but is the overhead of
throwing the exception cancelling out any benefit that I achieved by not
calling Contains()? To answer this question, you probably need to know how
frequently 'obj' is actually found in the Hashtable. For the sake of
argument, let's say 50%.
Thanks,
Jules
then presented with an object and *if* that object exists in the Hashtable,
I need to remove it from the table. I therefore have a lot of code that
looks like this:
if(ht.Contains(obj)){
ht.Remove(obj);
}
It strikes me that this code is somewhat inefficient because the
Hasthtable has to be searched *twice* : once for the Contains() call and
once for the Remove() call. So I thought maybe I should approach the
situation this way:
try{
ht.Remove(obj);
}
catch(Exception){}
Now I'm only searching the Hashtable once... but is the overhead of
throwing the exception cancelling out any benefit that I achieved by not
calling Contains()? To answer this question, you probably need to know how
frequently 'obj' is actually found in the Hashtable. For the sake of
argument, let's say 50%.
Thanks,
Jules