Click here to Skip to main content
15,867,999 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a sorted list that stores current values of 3 different data types. I also have a hash table that holds the previous value of each data point. I need to compare the newest value to the previous value and store any ones that change to a list. This has been working fine for months but a new requirement was added making some of the values = nothing and this is where my problems began

Previous code

For each de as DictionaryEntry in serialData<br />name = de.Key.Tostring<br />If ValueChanged(de.Value, hash.Item(name),de.value.GetType.Name = 1 Then<br />... add new value to list<br />... replace value is hash table with new value<br />End If<br /><br />Public Function ValueChanged(Byval val1 as Object, ByVal val2 as Object, ByVal typ as String) as Int16<br /><br />Select Case typ<br />Case "Single"<br />Dim newVal, lastVal as Single<br />newVal = val1<br />lastVal = val2<br /><br />If Not newVal = lastVal then <br />Return 1<br />Else <br />Return 0<br />End If<br /><br />Case "UInt32"<br />.... same<br /><br />Case "Int32"<br />.... same<br /><br />End Select<br />


For the new requirement, I added a If val1 = Nothing Then Return 1 but now this line returns true when val2 = 0.0 making the function return 1 which I dont want. 0.0 should not be equal to nothing. I also tried using Nullable types

Public shared Function HasChanged(ByVal val1 as Single, ByVal val2 as Nullable(Of Single)) As Int16 <br />If val2.HasValue Then <br />If val1 = val2 Then<br />Return 0 <br />Else <br />Return 1<br />End if <br />Else <br />Return 1<br />End if<br /><br />Public shared Function HasChanged(ByVal val1 as Single, ByVal val2 as Nullable(Of Int32)) As Int16 <br />If val2.HasValue Then <br />If val1 = val2 Then<br />Return 0 <br />Else <br />Return 1<br />End if <br />Else <br />Return 1<br />End if<br /><br />Public shared Function HasChanged(ByVal val1 as Single, ByVal val2 as Nullable(Of UInt32)) As Int16 <br />If val2.HasValue Then <br />If val1 = val2 Then<br />Return 0 <br />Else <br />Return 1<br />End if <br />Else <br />Return 1<br />End if<br />


but this increases my CPU from 3% to 56%! Any idea what I could be doing wrong. I am in a time crunch
Posted

1 solution

Hi,

nullable types don't come for free, they take more memory and more cycles, as they have to store and process the data that goes with the extra functionality (think of it as one extra boolean flag "hasValue",
it would double the footprint of an int, double the amount of data to be copied when performing nullable int=nullable int, etc).

you may have to account for changes in "hasValue" too; i.e. a variable that goes from "hasNoValue" to "hasValue" has definitely changed (and if you allow the reverse change, from "hasValue" to "hasNoValue", that would be a change too). So your history information should not be just "oldValue" but also "oldHasValue". Hence
Public shared Function HasChanged(ByVal val1 as Single, ByVal val2 as Nullable(Of Int32)) As Int16
IMO should be more like (I'm no VB expert!):
<br />Public shared Function HasChanged(ByVal val1 as Nullable(Of Int32), ByVal val2 as Nullable(Of Int32)) As Bool<br />    if val1.HasValue AND val2.HasValue return val1<>val2   ' value changed<br />    if val1.HasValue <> val2.HasValue return True  ' hasValue changed<br />    return False  ' was and is without value<br />End Function<br />


Some more remarks:
1. Try to avoid ToString. if Key is already a string, no need to call ToString on it.
2. I'm not sure having two different collections (one of them a HashTable/Dictionary) is wise; I would try and figure a way with just one. More in particular, if current value and previous value is a characteristic of your objects, I would incorporate that in the object itself, not delegate it to some HashTable. A simple For Each would then suffice.
3. While doing (2) I would not use nullable types, instead I would add an explicit bool flag to my objects. As I expect my code would be more efficient than the general-purpose nullable support (see first alinea).

:)

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900