简体   繁体   English

重置vb.net结构的最快方法是什么?

[英]what's the fastest way to reset a vb.net Structure?

For example... 例如...

    PUBLIC Structure System_Scoring_Structure
        dim     daily_evaluation_score         as double
        dim     weekly_evaluation_score        as double
        dim     monthly_evaluation_score       as double
        dim     yearly_evaluation_score        as double
End Structure

..after using this structure, I want to reset each variable to zero, before re-using it again for another data record to process. ..使用这种结构后,我想将每个变量重置为零,然后再次将其重新用于其他数据记录进行处理。 All I can think of is to set each variable to zero. 我能想到的就是将每个变量设置为零。 (My actual Structure has a lot more variables.) (我的实际结构有很多变量。)

Redim and Erase only work on arrays. Redim和Erase仅适用于阵列。

How about just new'ing it up? 刚刚更新怎么样?

Dim scores As System_Scoring_Structure = New System_Scoring_Structure

'  TODO:  Set the scores members and use it somewhere...
'  ...
'  now zero the structure out..
scores = New System_Scoring_Structure
'  it's reset and ready to be filled again.

I've just run a simplistic benchmark to see which is the quickest method. 我只是运行了一个简单的基准测试,看看哪种方法最快。

If any of your variables are reference types, I would recommend creating a class, definitely. 如果您的任何变量是引用类型,那么我肯定会建议您创建一个类。

Assuming that all the variables in your structure are value types, I compared the following two ways. 假定结构中的所有变量都是值类型,我比较了以下两种方法。

I created my own structure of value types with 27 variables of the Double type and wrote a method within the structure to set each double to 0 called .Reset 我用27个Double类型的变量创建了自己的值类型结构,并在该结构中编写了一个方法,将每个double都设置为0,称为.Reset

I then ran this code with a couple of stopwatches to see how long this took to complete 1000000000 times, and how long it took to simply assign as a new instance of the structure. 然后,我用几个秒表运行此代码,以了解完成1000000000次所需的时间,以及简单地将其分配为该结构的新实例所花费的时间。

My structure was called test - yes capitalization an all that ... 我的结构被称为test -是的,全部都用大写...

    Dim t1 As New Stopwatch
    Dim s As test
    Dim s1 As test
    t1.Start()
    For i As Integer = 1 To 1000000000
        s.reset()
    Next
    t1.Stop()
    Dim t2 As New Stopwatch
    t2.Start()

    For i As Integer = 1 To 1000000000
        s1 = New test
    Next
    t2.Stop()
    MessageBox.Show(t1.ElapsedMilliseconds & "   " & t2.ElapsedMilliseconds)

Executing the sub 1000000000 times took about 21 seconds, while executing the New assignment 1000000000 times took 24 seconds on my pc. 执行sub 1000000000次大约需要21秒,而执行New分配1000000000次需要24秒。 So there is a noticeable difference, but of your code isn't going to reset the variable an awful lot, it's down to personal taste. 因此,有一个明显的区别,但是您的代码不会将变量重设置得太多,这取决于个人喜好。 Memory consumption not significantly different so that's not a consideration either. 内存消耗没有显着差异,因此也无需考虑。

Out of interest, I changed the structure to a class which, according to Microsoft is the recommended way of create such a thing the if data takes up more than 16 bytes of memory, and ran the benchmark again. 出于兴趣,我将结构更改为一个类,根据Microsoft的建议,如果数据占用16个字节以上的内存,并再次运行基准测试,则可以创建此类。 This time is took about 19 seconds and 31 seconds respectively and the second loop used a couple of megabytes more memory. 这个时间分别花费了大约19秒和31秒,第二个循环使用了几兆字节的内存。

So .. up to you really. 所以..取决于您。

You can delegate this to a function or subroutine - but understand whether you want to adjust the original data (first example - ByRef), or replace the original data with a new copy (second example). 您可以将其委派给函数或子例程-但要了解是要调整原始数据(第一个示例-ByRef),还是要用新副本替换原始数据(第二个示例)。

Private sub Reset_system_scoring(ByRef sss as System_Scoring_Structure)
  sss.daily_evaluation_score = 0
  sss.weekly_evaluation_score = 0
  [...]
end sub

or 要么

Private function Reset_system_scoring() as System_Scoring_Structure
  dim sss as System_Scoring_Stucture
  sss.daily_evaluation_score = 0
  sss.weekly_evaluation_score = 0
  [...]
  return sss  ' OK - this is really ugly code.
end sub

As previously suggested - re New ing the structure could also work - this assumes that all variables inside will default to 0 unless you have a specific constructor. 正如上面所提到-再New荷兰国际集团的结构也能工作-这是假定所有变量里面将默认为0,除非你有特别的构造。

Refactoring the structure into a class should also be considered. 还应考虑将结构重构为一个类。 You can use it to store base data and then do internal calculations to get aggregated data. 您可以使用它来存储基本数据,然后进行内部计算以获取汇总数据。 This would also allow you to code in various methods, including Reset , which make your main code look neater. 这也将允许您使用各种方法进行编码,包括Reset ,这会使您的主代码看起来更整洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM