Efficiency Question regarding RaiseEvent in a Loop

Hello all,

I have what can be a very large loop that is processing over 50k interations. At the end I need the “Data results” send to a custom event definition that turns around and loads a listbox. Something like this.

for i = 0 to alot
Blah = process a bunch of functions(parms)

RaiseEvent CustomEvent(parms)
next i

My app is taking a while on anything over 20k iterations and I was wondering if I should not call RaiseEvent tied to populating a listbox every iteration vs. storing the results data say in an array or storage classes to then load the listbox all at one post processing?

Any advise is very much appreciated.

Thank you in advance.

Calling a method (which is what RaiseEvent is ultimately doing) involves overhead, so anything you can do to minimize that will likely improve performance.

What happens if you comment out RaiseEvent and not use the listbox at all? Is it appreciably faster? Have you used the profiler on it?

Is this in a thread? If not, it probably should be. And once you’re doing that the UI will remain responsive.

However, Cocoa and Windows threads that accessing UI are anathema now (since they raise ThreadAccessingUI Exceptions). In which case look at the Task example in the thread examples folder to see how to do this.

thanks Guys. Tim - It is a bit more responsive, but still churns so I will use the profiler to check out what I can make more efficient. Bob thanks as I have been planning on adding a Thread to give the user a “work” status bar :wink:

Thanks again guys! Ill post my profiler stuff in a bit as I am not the best with that yet. :slight_smile: Thanks!

Seems like I am hitting all my functions everytime as expected. I have a question for converting Binary words into Decimal. Would using Memory Blocks increase my speed? I hit this function 262k times so anything would help :slight_smile:
Thanks!

  // Convert 32 Bit Word into Decimal and in 4 Octets separated by a .
  Dim FirstOctet as Integer = Val("&b"+Inbound32BitWord.Mid(1,8))
  Dim SecondOctet as Integer =Val("&b"+Inbound32BitWord.Mid(9,8))
  Dim ThirdOctet as Integer = Val("&b"+Inbound32BitWord.Mid(17,8))
  Dim FourthOctet as Integer = Val("&b"+Inbound32BitWord.Mid(25,8))
  
  Dim DecimalAddress as String = Str(FirstOctet) + "." + Str(SecondOctet) + "." + Str(ThirdOctet) + "." + Str(FourthOctet)
  
  Return DecimalAddress

Also I run 65535 Iterations through the Loop. I am hitting this method 4 times per iteration FYI :slight_smile: Thanks again.

Thread, Timer, Queue. Have the CustomEvent add items to a pre-allocated queue (array), keep a front and back index pointer to the queue so you’re not copying/moving elements. Have the Timer grab a pre-determined number of items from the queue, add to listbox.

Before doing anything, benchmark with a CustomEvent that just adds a simple text row to the ListBox. If that takes a long time, you’ll be doing more to manage UI than calculate stuff. Entirely possible.

Thanks Brad! Ill try those recommendations.

[quote=66223:@Mike Cotrone]Seems like I am hitting all my functions everytime as expected. I have a question for converting Binary words into Decimal. Would using Memory Blocks increase my speed? I hit this function 262k times so anything would help :slight_smile:
Thanks!

  // Convert 32 Bit Word into Decimal and in 4 Octets separated by a .
  Dim FirstOctet as Integer = Val("&b"+Inbound32BitWord.Mid(1,8))
  Dim SecondOctet as Integer =Val("&b"+Inbound32BitWord.Mid(9,8))
  Dim ThirdOctet as Integer = Val("&b"+Inbound32BitWord.Mid(17,8))
  Dim FourthOctet as Integer = Val("&b"+Inbound32BitWord.Mid(25,8))
  
  Dim DecimalAddress as String = Str(FirstOctet) + "." + Str(SecondOctet) + "." + Str(ThirdOctet) + "." + Str(FourthOctet)
  
  Return DecimalAddress

Also I run 65535 Iterations through the Loop. I am hitting this method 4 times per iteration FYI :slight_smile: Thanks again.[/quote]
Where does this data come from? Do you have control over it at all? This looks like about the least efficient way to represent it. (Assuming I understand correctly - you’re getting a string like “1001110010100010101011010101…”?)

Yes a 32 Bit binary word from the GUI IP address a user enters.

I an using a Decimal to Binary conversion in 32 bit words using:

  Private Function fCalculateBinaryWithPadding_OneOctet(DecimalInput as Integer) As String
  // Convert the Inbound Integer (Decimal) into Binary (String)
  Dim BinaryConversion as String = Bin(DecimalInput)
  
  // Now Determine if we will need to Pad the Binary String with leading 0's since Xojo's Bin Function strips all Leading 0's
  Dim BinaryConversion_Len as Integer = BinaryConversion.LenB
  
  Select Case BinaryConversion_Len
  Case 0
    BinaryConversion = "00000000"+BinaryConversion
  Case 1
    BinaryConversion = "0000000"+BinaryConversion
  Case 2
    BinaryConversion = "000000"+BinaryConversion
  Case 3
    BinaryConversion = "00000"+BinaryConversion
  Case 4
    BinaryConversion = "0000"+BinaryConversion
  Case 5
    BinaryConversion = "000"+BinaryConversion
  Case 6
    BinaryConversion = "00"+BinaryConversion
  Case 7
    BinaryConversion = "0"+BinaryConversion
  End Select
  
  Return BinaryConversion
End Function

Wait, you’re converting it from decimal to binary just to convert it back?

Yes :frowning: It starts as Decimal and then for some functions I need to calculate it in binary form and then back to user friendly decimal.

I am trying to refactor it since I fixed some math issues I had tonight. >-)

Sounds like you need a class that stores both forms so you don’t have to keep recalculating.

I think if I can keep it in decimal the whole time that will increase efficiency big time.

The issue is that I would have to use way to many case/if statements if I stay in decimal vs. binary calculations…

Like what?

Representing the # of Bits to use in formula calculations would like this … Which isn’t too bad. However the other functions I wrote will require a whole road of rethinking to be done solely in decimal. When I teach subnetting its always in binary and “short hand” tricks are taught in decimal.

  Dim n, n2,n3,n4,m2,m3,m4 as Integer
  
  n2 = SubnetMask2Dec
  Select Case n2
  Case 0
    m2 = 0
  Case 128
    m2 = 1
  Case 192
    m2 = 2
  Case 224
    m2 = 3
  Case 240
    m2 = 4
  Case 248
    m2 = 5
  Case 252
    m2 = 6
  Case 254
    m2 = 7
  Case 255
    m2 = 8
  End Select
  
  n3 = SubnetMask3Dec
  
  Select Case n3
  Case 0
    m3 = 0
  Case 128
    m3 = 1
  Case 192
    m3 = 2
  Case 224
    m3 = 3
  Case 240
    m3 = 4
  Case 248
    m3 = 5
  Case 252
    m3 = 6
  Case 254
    m3 = 7
  Case 255
    m3 = 8
  End Select
  
  n4 = SubnetMask4Dec
  Select Case n4
  Case 0
    m4 = 0
  Case 128
    m4 = 1
  Case 192
    m4 = 2
  Case 224
    m4 = 3
  Case 240
    m4 = 4
  Case 248
    m4 = 5
  Case 252
    m4 = 6
  Case 254
    m4 = 7
  Case 255
    m4 = 8
  End Select
  
  Return m2+m3+m4

I see. But as a string representation of binary, it’s easy to count to the "1"s, is that it?

I’m just trying to wrap my mind around under what conditions would it ever make sense to convert an integer value to a 32-character string representation. Mike, I don’t mean to be rude, but I think you may be missing some fundamental concept with regard to 32-bit integer values. This decimal vs binary dichotomy sounds a bit off. But maybe I’m just not understanding something.