filling a bitvector32 in a loop

Dear all,

I can fill a bitvector32 like this:

Dim InBytes As Int32

dim i

for i = 1 to 1000000

InBytes = i 'for example

Dim InBitVector As New System.Collections.Specialized.BitVector32(InBytes)

next i

and then I can read out the individual bits (need this for a graphics conversion).

Problem: this gets VERY slow, and I trailed that to the Dim InBitVector statement.

Is there ANY way to assign an Int32 number to the InBitVector WITHOUT using the dim statement

Thanks, Kees



Answer this question

filling a bitvector32 in a loop

  • Hibernating Bear

    Hi Renee (not a guy, I guess, from the double 'e' ),

    I am unclear how the bitconvertor.ToInt16 is going to help.

    I was aware of the bitconvertor and I knew a bit about shifting and ORing (I am an absolute beginner but try to not botter you guys (M/F) too much so I read the helpfile, begin-to-end. Which is not to say that I remember everything.

    But if you could drop a little hint than I try to work it out.

    Thanks, Kees


  • xlordt

    Yes, that is exactly the idea.

    Kesim


  • MuscleHead

    It's standard coding practice Kesim. What is not standard though here is your reply with hard data that other people use.

    Thank you.



  • x646d63

    Ok,

    You're right I think we need to back up and develop a common vocabulary.

    It looks to me that you trying to extract various bit fields out of bytes and combine them into another integer

    You see, I'm having a little difficult understanding what you are trying to do, not in scientific graphics but in terms of computer language.

    Am I correct in understanding that you are trying to extract bitfields out of bytes and to recombine them



  • Beth31

    I think this is what you want.

    Private Function BuildUint(ByVal Input As Integer) As UInt32

    Dim Out As UInt32

    If (Input And &H20000) <> 0 Then Out = 1 'bit 1 byte2 -> bit 0 byte 0

    If (Input And &H10000) <> 0 Then Out += 2 'bit 0 byte2 -> bit 1 byte 0

    If (Input And &H8000) <> 0 Then Out += 4 'bit 7 byte1 -> bit 2 byte 0

    If (Input And &H4000) <> 0 Then Out += 8 'bit 6 byte1 -> bit 3 byte 0

    If (Input And &H2000) <> 0 Then Out += &H10 'bit 5 byte1 -> bit 4 byte 0

    If (Input And &H1000) <> 0 Then Out += &H20 'bit 4 byte1 -> bit 5 byte 0

    If (Input And &H800) <> 0 Then Out += &H40 'bit 3 byte1 -> bit 6 byte 0

    If (Input And &H400) <> 0 Then Out += &H80 'bit 2 byte1 -> bit 7 byte 0

    If (Input And &H80) <> 0 Then Out += &H100 'bit 7 byte0 -> bit 0 byte 1

    If (Input And &H40) <> 0 Then Out += &H200 'bit 6 byte0 -> bit 1 byte 1

    If (Input And &H20) <> 0 Then Out += &HF400 'bit 5 byte0 -> bit 2 byte 1

    If (Input And &H10) <> 0 Then Out += &H800 'bit 4 byte0 -> bit 3 byte 1

    Return Out

    End Function



  • Chase M

    Excellent, Renee,

    depending on the picturesize (there is also some other stuff in the subroutine) I clocked 10-fold to over a 100-fold speed improvement in my application. Now loads a 120-kb 12 bits picture 20 times in ~50 ms, rather than several seconds.

    Thanks! Kees


  • Bastian W.

    Not sure that's even necessary. I get 4.5 microseconds per loop on my pokey laptop in the release build. But 75 microseconds per loop in the IDE, s/he'll notice that.

    Loop I tried:
    Dim mMember As Boolean

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim tick As Integer = Environment.TickCount
    For ix As Integer = 1 To 100000000
    Dim value As Integer = ix
    Dim vect As New BitVector32(value)
    mMember = vect.Item(0)
    Next
    tick = Environment.TickCount - tick
    Label1.Text = CStr(tick)



  • Troy Lundin

    Perhaps it's wise to say what I want in the first place.

    Perhaps you guys even know a way to prevent extensive coding in solving this problem.

    I have 12-bit gray-value scientific pictures that however are loaded by Freeimage in an odd 32-bit format. I looked and looked and saw no other way to extract the scientific data out it then what I talked about earlier.

    Format:

    Byte 1 : GreyBit 9 10 11 12 N N N N (N meaning: not used)

    Byte 2: GreyBit 3 4 5 6 7 8 N N

    Byte 3: GreyBit N N N N 1 2 N N

    Byte 4: GreyBit N N N N N N N N

    To me this looks really odd but I guess I am a beginner........

    So I have to extract the bits in good order into an Int16 which will be used for scientific calculations, and which will also be converted to Byte (drop lowest 4 bits) for display in a picturebox.

    The best thing I could think of is to make a second bitvector32(0), and then fil the right-most 12 bits with true or false depending on the mentioned bits in InBitVector like:

    Bitvector2.item(31) = InBitVector2.Item(3)

    bitvector2.item(30) = inbitvector2.item(2) ........etc for the other 10 bits. then

    get the number with 'inbitvector.data'

    This seems to go pretty fast, but perhaps you know a more elegant solution

    Also: nobugz, do you have a link to a page describing that timing trick with 'tickcount' that you use I used

    Public Function kjStartTime() As Double 'kj, juli 2006

    'Usage: a=kjStartTime() stores time in 0.1us in Double a

    Return My.Computer.Clock.LocalTime.ToBinary

    End Function

    Public Function kjStopTime(ByVal StartTime) As Double 'kj, juli 2006

    'Usage: label1.text=kjStopTime(a). companion to kjStartTime

    Return (My.Computer.Clock.LocalTime.ToBinary - StartTime) * 0.0000001

    End Function

    and I am wondering whether with tickcount I get the same improvement.

    Thanks! Kees


  • DogObsessedperson

    Key,

    Everyone here is not a guy.

    it looks like to me as if you are interested in bit manipulation.

    take a look at System.BitConverter in your objectbrowser at the BitConverter

    the System.BitConverter.ToInt16 property should really interest you and then learn about the shift operations in masks and logical ORing. Those are the most powerful tools in doing what you are doing.



  • swatmajor1

    Would he be able to get away with out using a NEW in the loop



  • Jewelfire1

    Hi Renee (not a guy, I guess, from the double 'e' ),

    I am unclear how the bitconvertor.ToInt16 is going to help.

    I was aware of the bitconvertor and I knew a bit about shifting and ORing (I am an absolute beginner but try to not bother you guys (M/F) too much so I read the helpfile, begin-to-end. Which is not to say that I remember everything.)

    But if you could drop a little hint than I try to work it out.

    Thanks, Kees


  • Christian Sparre

    Hmm, odd. It shouldn't make any difference and it didn't when I tried it. There is however a huge difference between running the release mode or debugging with the IDE, I measured a factor of 16.



  • R.Tutus

    You could probably make it faster by using bit-masks. You should try it though, BitVector32 is quite efficient:
    Bitvector2.Item(31) = CBool(InBytes And &H40000)
    Bitvector2.Item(30) = CBool(InBytes And &H20000)
    ...
    Bitvector2.Item(20) = CBool(InBytes And &H10)

    System.Environment.TickCount gives the current "system tick" in milliseconds, accurate to about +/- 15 msec. For better resolution and accuracy, use the System.Diagnostics.StopWatch class:

    Dim watch As New System.Diagnostics.Stopwatch
    watch.Start()
    For ix As Integer = 1 To 1000000
    ...
    Next
    watch.Stop()
    Label1.Text = String.Format("{0:N3}", watch.ElapsedTicks / CDbl(System.Diagnostics.Stopwatch.Frequency))



  • TheresaKad

    Dim InBitVector As System.Collections.Specialized.BitVector32

    for i = 1 to 1000000

    InBytes = i 'for example

    InBitVector = New System.Collections.Specialized.BitVector32(InBytes)

    next i

    Will give you some gain in speed.



  • filling a bitvector32 in a loop