使用Nullable增加CPU [英] CPU increase using Nullable

查看:65
本文介绍了使用Nullable增加CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个排序的列表,其中存储了3种不同数据类型的当前值.我还有一个散列表,其中包含每个数据点的先前值.我需要将最新的值与先前的值进行比较,并存储所有更改为列表的值.这已经工作了好几个月了,但是添加了一个新要求,使某些值= no,这就是我的问题开始的地方.

上一个代码

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 />



对于新要求,我添加了If val1 = Nothing Then Return 1,但是现在当val2 = 0.0时此行返回true,从而使该函数返回我不想要的1. 0.0不应该等于空.我还尝试使用可空类型



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 />



但这会将我的CPU从3%增加到56%!知道我可能做错了什么.我处于时间紧迫状态



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

推荐答案



可空类型不是免费提供的,它们需要更多的内存和更多的周期,因为他们必须存储和处理具有额外功能的数据(将其视为一个额外的布尔标志"hasValue",
它将使int的占用空间增加一倍,在执行可为空值时要复制的数据量增加一倍) int =可为null的int等).

您可能还必须考虑"hasValue"中的更改;即,从"hasNoValue"到"hasValue"的变量肯定已更改(如果您允许从"hasValue"到"hasNoValue"的反向更改,那也将是更改).因此,您的历史记录信息不仅应该是"oldValue",还应该是"oldHasValue".因此,
Public shared Function HasChanged(ByVal val1 as Single, ByVal val2 as Nullable(Of Int32)) As Int16
IMO应该更像(我不是VB专家!):
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 />



更多说明:
1.尽量避免使用ToString.如果Key已经是一个字符串,则无需在其上调用ToString.
2.我不确定拥有两个不同的集合(其中一个是HashTable/Dictionary)是否明智;我会尝试只用一种方法.更具体地说,如果当前值和先前值是对象的特征,那么我会将其合并到对象本身中,而不是将其委托给某些HashTable.一个简单的For Each就足够了.
3.在做(2)时,我不会使用可为空的类型,而是向我的对象添加一个显式的bool标志.正如我期望的那样,我的代码将比通用可空支持更有效(请参阅第一个alinea).

:)



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).

:)


这篇关于使用Nullable增加CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆