Page 1 of 3 123 LastLast
Results 1 to 40 of 91

Thread: Nibbles, bit operations and C dll's [RESOLVED]

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved Nibbles, bit operations and C dll's [RESOLVED]

    In order to decode a file I must work with nibbles, so I have to do some byte shifting and-ing and or-ing. Because the files are large, around 10 Mb, speed really counts. I thought it would be faster if I could directly work on nibbles, but no such data type exists in VB. Could there be some way around? Btw I do the bit shifting by multiplying or dividing by powers of 2 as there's no shift operator as far as I know.
    Last edited by krtxmrtz; Apr 21st, 2005 at 05:24 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Nibbles!

    Does your data allow you to use a "brute-force" method? That is, if you only have a limited number of values in your data set you can hard-code the processing for each value.

    Does this make sense?
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by trisuglow
    Does your data allow you to use a "brute-force" method? That is, if you only have a limited number of values in your data set you can hard-code the processing for each value.

    Does this make sense?
    The number of values is limited, of course, but it can be different according to the file I'm working with. But I'm afraid I don't know what you mean by that hard-coding, can you set an example?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Nibbles!

    I was just thinking that if there was some kind of relationship between the lower and upper nybbles then you could know the upper nybble just by looking at the lower nybble. It sounds like this isn't going to work for you though. Since its not going to be applicable I can't give an example. Sorry.
    This world is not my home. I'm just passing through.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Nibbles!

    Nibbles are very easy to work with...

    00001111 = 15
    11110000 = 240

    Those are the masks used to pull just the lower order or high order nibble out of the byte.

    lngLowOrder = lngValue and 15 ' lngLowOrder now has only the low order nibble
    lngHighOrder = (lngValue and 240) / 16 ' lngHighOrder now has the high order nibble and it's bit shifted down

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by szlamany
    Nibbles are very easy to work with...

    00001111 = 15
    11110000 = 240

    Those are the masks used to pull just the lower order or high order nibble out of the byte.

    lngLowOrder = lngValue and 15 ' lngLowOrder now has only the low order nibble
    lngHighOrder = (lngValue and 240) / 16 ' lngHighOrder now has the high order nibble and it's bit shifted down
    Yes, that's exactly how I'm doing it, but the aim of this post was rather to make sure there wasn't some trick to achieve the same result but faster. The thing is I'm trying to optimize in terms of speed this file decoding I've mentioned in the first post, and one of the possibilities was better manipulation of nibbles.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Nibbles!

    That is the best you are going to get out of a language like VB.

    It the nibble work you need to do can be done in ASM (or maybe C) then a routine in that language might be better.

    But the last thing you want to do is start calling a function (expensive to call) just to work a byte - it would have to do all the work before coming back to VB (or at least a large part of it).

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by szlamany
    That is the best you are going to get out of a language like VB.

    It the nibble work you need to do can be done in ASM (or maybe C) then a routine in that language might be better.

    But the last thing you want to do is start calling a function (expensive to call) just to work a byte - it would have to do all the work before coming back to VB (or at least a large part of it).
    A number of years ago I used Microsoft C 6.0 in a IDE running on DOS. But I don't know how I'd call a sub written in C from VB. Would I have to make a DLL or something?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles!

    Yes, make a DLL with C. It is likely to be 10-100 times faster to do string parsing in C than in VB.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  10. #10

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Dave Sell
    Yes, make a DLL with C. It is likely to be 10-100 times faster to do string parsing in C than in VB.
    Well, it's not exactly string parsing what I've got to do but the nibble operations mentioned above.
    Now I come to think of it, I know how to make a dll in the IDE of VB6 but how would I go about it in c? Do I have to install the VC part of Visual Studio or is there a more straightforward way in the old DOS-C itself?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  11. #11
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    Well, it's not exactly string parsing what I've got to do but the nibble operations mentioned above.
    Now I come to think of it, I know how to make a dll in the IDE of VB6 but how would I go about it in c? Do I have to install the VC part of Visual Studio or is there a more straightforward way in the old DOS-C itself?
    Anything resembling traversing a ginormous array of bytes is considered string-parsing (an old C nomenclature).

    I am not an expert making C or C++ DLLs but I have done it a few times in the past. I searched around and found plenty of tutorials and followed them. They always had me using VS C++ 6. I recommend you go that route also. C++ is very fast - I consider it the same speed as C when all you are doing is string-parsing.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  12. #12

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Dave Sell
    They always had me using VS C++ 6. I recommend you go that route also.
    OK, I'll have a go at it. Thanks.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  13. #13
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles!

    Quote Originally Posted by szlamany
    But the last thing you want to do is start calling a function (expensive to call) just to work a byte - it would have to do all the work before coming back to VB (or at least a large part of it).
    I'm sure you know this, krtxmrtz, but szlamany makes an excellent point. Do not make a function that accepts data byte per byte; rather, give the DLL your entire buffer, or at least a KiloByte at a time...
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  14. #14

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Dave Sell
    I'm sure you know this, krtxmrtz, but szlamany makes an excellent point. Do not make a function that accepts data byte per byte; rather, give the DLL your entire buffer, or at least a KiloByte at a time...
    Yes I was aware of this but it's one of those things you so easily tend to overlook so, thanks for reminding me of it.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  15. #15
    Junior Member
    Join Date
    Feb 2002
    Posts
    23

    Re: Nibbles!

    Quote Originally Posted by Dave Sell
    Yes, make a DLL with C. It is likely to be 10-100 times faster to do string parsing in C than in VB.
    Hi,

    Just curious as to WHY "C" is faster than VB. I know it's true, I've just never learned the why. Any insight or any links to info on this would be appreciated.

  16. #16

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by SideOut
    Hi,
    Just curious as to WHY "C" is faster than VB.
    I'm curious too, I don't understand why VB compilers couldn't be designed to be as efficient as c compilers. What's he difference?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Nibbles!

    Quote Originally Posted by SideOut
    Hi,

    Just curious as to WHY "C" is faster than VB. I know it's true, I've just never learned the why. Any insight or any links to info on this would be appreciated.
    I don't use C, but I imagine it's how the language can look at a spot in memory.

    Back in our mainframe BASIC days we wrote little PEEK and POKE functions - pass the function the "location" of a string and the offset into that string you wanted to touch and it would pull a byte, word or longword from it.

    In VB (I assume) the only way to do that is to LEFT/MID/RIGHT the string, which makes a new string (probably an 8 byte memory structure just to hold the byte, word or longword) and then some other FUNCTION call (ASC?) to convert it to a value.

    That type of requirement to get at a memory location to play with bytes is expensive.

    I would choose to develop the routines needed in this thread in ASM myself - but from what I hear C can imbed ASM and ASM-like call directly.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  18. #18
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    Could you possibly post some of the code you're using? It's impossible for us to come up with any optimization without seeing the code you're using.

  19. #19
    Junior Member
    Join Date
    Feb 2002
    Posts
    23

    Re: Nibbles!

    Quote Originally Posted by szlamany
    I don't use C, but I imagine it's how the language can look at a spot in memory.

    In VB (I assume) the only way to do that is to LEFT/MID/RIGHT the string, which makes a new string (probably an 8 byte memory structure just to hold the byte, word or longword) and then some other FUNCTION call (ASC?) to convert it to a value.

    That type of requirement to get at a memory location to play with bytes is expensive.
    Ahhhhh... So it's this kind of overhead that does it !

    Thankyou - Very well put.

  20. #20
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Nibbles!

    Quote Originally Posted by SideOut
    Ahhhhh... So it's this kind of overhead that does it !

    Thankyou - Very well put.
    Yes - what seems so routine in higher level languages - a simple call to a VB function, or a SUB/Function in a module you write yourself causes all kinds of memory framing action so that "new variable" space can be created - arguments passed (by ref less expensive - by value more expensive).

    It's so common on this forum for suggestions to use SPLIT to find things in a string. SPLIT creates an array of new strings - that's expensive.

    It's also common in the VB world to use REDIM with PRESERVE - that's extremely expensive as well. Most likely moving the entire array to a new large or smaller location.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  21. #21

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Joacim Andersson
    Could you possibly post some of the code you're using?
    This is the core of it.

    What this does is, it reads a file where a matrix of values z=f(x,y) is
    stored. Now, these values have a 12-bit precision and in order to save
    some space, they are packed in 3 bytes -call them b0, b1 and b2- in
    this fashion:
    The first 12-bit value has its 8 low order bits in b0 and its 4 high order
    bits in the high order nibble of b2. The second 12-bit value has its 8 low
    order bits in b1 and its 4 high order bits in the low order nibble of b2.

    I hope this is clear at least from the attached drawing (sorry about this large size, I didn't mean to make it like that)
    VB Code:
    1. Const HeaderSize = 2048
    2. Dim nSize As Long
    3. Dim maxval As Single
    4. Dim DataByte() As Byte
    5. Dim Val12bit() As Integer
    6. Dim NormdData() As Single
    7. Dim GreyLevel() As Byte
    8. Public Sub Read_the_file()
    9.     'The following sub reads 2048 bytes of header data
    10.     'I don't include it for it's irrelevant
    11.     Get_Header
    12.     'This sub is pretty fast, (possibly) no need to optimize here
    13.     Get_Rest
    14.     Process_Data
    15. End Sub
    16. Private Sub Get_Rest()
    17.     Dim ff As Integer
    18.        
    19.     ff = FreeFile
    20.     Open FileName For Binary As #ff
    21.         nSize = LOF(ff)
    22.         'HeaderSize has been defined elsewhere
    23.         'DataByte is grouped in 3 consecutive bytes
    24.         'in which two 12-bit values are packed
    25.         ReDim DataByte(0 To 2, 0 To (nSize - HeaderSize) \ 3 - 1)
    26.         Seek #ff, HeaderSize + 1
    27.         Get #ff, , DataByte
    28.     Close #ff
    29. End Sub
    30. Public Sub Process_the_Data()
    31.  
    32.     Dim i As Long
    33.     Dim j As Long
    34.     Dim uLim As Long
    35.    
    36.     uLim = 2 * (nSize - HeaderSize) \ 3 - 1
    37.    
    38.     ReDim Val12bit(uLim)
    39.     ReDim GreyLevel(uLim)
    40.     ReDim NormdData(uLim)
    41.     maxval = 0
    42.    
    43.     For i = 0 To (nSize - HeaderSize) \ 3 - 1
    44.         j = 2 * i
    45.         'The first 12-bit value is built from the high order nibble of
    46.         'the 3rd DataByte followed by the entire first DataByte
    47.         Val12bit(j) = (DataByte(2, i) And &HF0) * 16 Or DataByte(0, i)
    48.         'The second 12-bit value is built from the low order nibble of
    49.         'the 3rd DataByte followed by the entire second DataByte
    50.         Val12bit(j + 1) = (DataByte(2, i) And &HF) * 256 Or DataByte(1, i)
    51.        
    52.         'The Val12bit values are scaled here and their
    53.         'maximum is sought for posterior normalization
    54.         NormdData(j) = Val12bit(j) / 1000
    55.         If maxval < NormdData(j) Then maxval = NormdData(j)
    56.         NormdData(j + 1) = Val12bit(j + 1) / 1000
    57.         If maxval < NormdData(j + 1) Then maxval = NormdData(j + 1)
    58.     Next
    59.    
    60.     'The data represent the values of a function f(x,y)
    61.     'Although they have 12-bit precision, it's convenient
    62.     'to plot them on a picturebox in a gray scale
    63.     'In the following loop they are compressed to 8 bit
    64.     For i = 0 To (nSize - HeaderSize) \ 3 - 1
    65.         j = 2 * i
    66.         'Maximum value should be black and 0 (minimum) white
    67.         GreyLevel(j) = 255 - Int(255 * NormdData(j) / maxval)
    68.         GreyLevel(j + 1) = 255 - Int(255 * NormdData(j + 1) / maxval)
    69.     Next
    70.    
    71.     'I have left out the actual code for plotting to a picturebox
    72.     'For that I'm using a class someone provided some time
    73.     'ago in this forum with DIB related functions, faster than
    74.     'SetPixel
    75. End Sub
    Attached Images Attached Images  
    Last edited by krtxmrtz; Apr 15th, 2005 at 08:22 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  22. #22

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    By the way...
    If I am to write one or two DLLs in C I'd like someone to recommend a tutorial, but not just a C tutorial as I more or less know the syntax and structure of the language. What I'd like is some push to get blitz started in the VC environment of Visual Studio.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  23. #23
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    I did some changes to your code but since I can't test it it's hard if these will make any noticable differences (even though it should be faster).
    VB Code:
    1. Public Sub Process_the_Data()
    2.     Dim i As Long
    3.     Dim j As Long
    4.     Dim uLim As Long
    5.     'I added this:
    6.     Dim nMax As Long
    7.     Dim nUBound As Long
    8.    
    9.     nUBound = 2 * (nSize - HeaderSize) \ 3 - 1
    10.     'If the following calculation really correct?
    11.     ' uLim = 2 * (nSize - HeaderSize) \ 3 - 1
    12.     'Should it not be:
    13.     ' uLim = 2 * ((nSize - HearderSize) \ 3 - 1)
    14.     'At least I will assume that:
    15.     uLim = 2 * nUBound
    16.    
    17.     ReDim val12bit(uLim)
    18.     ReDim GreyLevel(uLim)
    19.     ReDim NormdData(uLim)
    20.     maxval = 0
    21.    
    22.     For i = 0 To nUBound '(nSize - HeaderSize) \ 3 - 1
    23.         j = 2 * i
    24.         'The first 12-bit value is built from the high order nibble of
    25.         'the 3rd DataByte followed by the entire first DataByte
    26.         val12bit(j) = (DataByte(2, i) And &HF0) * 16 Or DataByte(0, i)
    27.         'The second 12-bit value is built from the low order nibble of
    28.         'the 3rd DataByte followed by the entire second DataByte
    29.         val12bit(j + 1) = (DataByte(2, i) And &HF) * 256 Or DataByte(1, i)
    30.        
    31.         'The Val12bit values are scaled here and their
    32.         'maximum is sought for posterior normalization
    33.         If nMax < var12bit(j) Then nMax = var12bit(j)
    34.         If nMax < var12bit(j + 1) Then nMax = var12bit(j + 1)
    35. '        NormdData(j) = val12bit(j) / 1000!
    36. '        If maxval < NormdData(j) Then maxval = NormdData(j)
    37. '        NormdData(j + 1) = Val12bit(j + 1) / 1000
    38. '        If maxval < NormdData(j + 1) Then maxval = NormdData(j + 1)
    39.     Next
    40.     'Only do the following calculation once if you really need it later
    41.     maxval = nMax / 1000!
    42.    
    43.     'The data represent the values of a function f(x,y)
    44.     'Although they have 12-bit precision, it's convenient
    45.     'to plot them on a picturebox in a gray scale
    46.     'In the following loop they are compressed to 8 bit
    47.     For i = 0 To nUBound '(nSize - HeaderSize) \ 3 - 1
    48.         j = 2 * i
    49.         'Maximum value should be black and 0 (minimum) white
    50.         'Do an integer division with 1 since it is faster then the Int function
    51.         GreyLevel(j) = 255 - ((255& * val12bit(j) / nMax) - 0.5) \ 1
    52.         GreyLevel(j + 1) = 255 - ((255& * val12bit(j + 1) / nMax) - 0.5) \ 1
    53.     Next
    54.    
    55.     'I have left out the actual code for plotting to a picturebox
    56.     'For that I'm using a class someone provided some time
    57.     'ago in this forum with DIB related functions, faster than
    58.     'SetPixel
    59. End Sub
    The largest different here is that I took away the NormdData array. I don't know if you need it later on in which case you can simply remove the comment. Some other improvments you might want to think about is to remove the Array bound check, the Integer overflow check, and the Floating point error check. Of course those optimisations will not be noticable until you compile the program.

  24. #24

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Joacim Andersson
    I did some changes to your code but since I can't test it it's hard if these will make any noticable differences (even though it should be faster)
    Well, I'll stick this into my own code and see what comes out from the tests. In the meantime I've been playing around and I'm wondering if anything is to be gained by using a 1D rather than 2D DataByte array, but that'll take me some time to implement. I'll keep you updated...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  25. #25

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Joacim Andersson
    'Is the following calculation really correct?
    ' uLim = 2 * (nSize - HeaderSize) \ 3 - 1
    'Should it not be:
    ' uLim = 2 * ((nSize - HearderSize) \ 3 - 1)
    I think it's correct because if you call

    N = nSize - HeaderSize

    then, this N is the total number of data bytes. For every 3 of these, 2 Val12bit values are built so, first you divide by 3 to get the number of triads,

    N\3 triads

    from each triad you get 2 Val12bit values so, the number of those is

    2*N\3

    and finally you've got to redim from 0 to 2*N\3 -1

    This is one reason why I find this feature confusing, starting arrays at 0 rather than at 1, yet I've been using 0-based arrays recently, I suppose it's more convenient for me to adjust to what most people do.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  26. #26
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    I've been playing around and I'm wondering if anything is to be gained by using a 1D rather than 2D DataByte array
    You might notice that it's running faster in the IDE but I doubt very much that it would make any difference when you've compiled it to native code since the machine code doesn't know the difference between a 1D and a 2D array

    That means that the compiler will probably create the same native code regardless of how many dimensions your array have.

  27. #27
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    I think it's correct because if you call .....
    Yeah you're right. I wasn't thinking correctly since the array is zero based. Change this line (if you're using my code)
    VB Code:
    1. uLim = 2 * nUBound
    2. 'change that to
    3. uLim = 2 * nUBound + 1

  28. #28
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Why is C faster than VB

    To answer the question why C is faster than VB, C has no overhead of Classes, COM, goofy data structures that represent a String.

    In VB 6, a string is a pointer to a structure with a size variable and then another pointer to the actual array of bytes. Also, you cannot just talk about the byte at position 6. You have to call functions that call other functions and make new data structures, and the list goes on and on.

    In C, you start with an array of bytes. You can talk about each byte in the array as an integer offset to the array. It is the fastest way possible to traverse an array byte by byte. No goofy VB overhead.

    For pure string-parsing, nothing beats C, not even assembler.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  29. #29
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles!

    I don't know if this helps you or not, but here is one way you can create your C++ dll from VC++
    let's assume dll is called MyDll.dll
    Create a new Win32 dll
    Add the following code to the cpp file
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    /******************************************************************************
    declare Sub ProcessData Lib "MyDll.dll" (DataByte as Byte, Val12bit as Long, NormdData as Single, GreyLevel as Byte, ByVal BufferSize as Long) 
    ******************************************************************************/
    __declspec( dllexport ) void _stdcall ProcessData (char DataByte[][3], short Val12bit[], float NormdData[], char GreyLevel[], long BufferSize)
    
    {
    //OK do your magic
    
    return;
    }
    Notice that the rows and columns are reversed in C++
    Then from VB declare your new sub
    VB Code:
    1. declare Sub ProcessData Lib "MyDll.dll" ( _
    2.   DataByte as Byte, _
    3.   Val12bit as Long, _
    4.   NormdData as Single, _
    5.   GreyLevel as Byte, _
    6.   ByVal BufferSize as Long _
    7. )
    And call it like so
    VB Code:
    1. Call ProcessData(DataByte(0,0), Val12bit(0), NormdData(0), GreyLevel(0), BufferSize)
    Also add a MyDll.def file to the C++ project
    Code:
    EXPORTS
     ProcessData

    If you need help translating the magic I can help too.
    Last edited by moeur; Apr 15th, 2005 at 04:29 PM.

  30. #30

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Why is C faster than VB

    Quote Originally Posted by Dave Sell
    For pure string-parsing, nothing beats C, not even assembler.
    I find this quite surprising not to say hard to believe. If c is so efficient at string parsing you'd only have to take the compiled c code and convert it to assembler, on the basis that each machine instruction corresponds to an actual assembler instruction... But my assembler days belong to the past and to the another computer architecture so I may overlook some of the facts.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  31. #31
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Why is C faster than VB

    Quote Originally Posted by krtxmrtz
    I find this quite surprising not to say hard to believe. If c is so efficient at string parsing you'd only have to take the compiled c code and convert it to assembler, on the basis that each machine instruction corresponds to an actual assembler instruction...
    If you take a string parsing routine written in C, say a for loop using integer offsets to the base address of the string (array of bytes), and then decompile it to assembler, you will get about a 1-1 ratio of C code to assembler code. In other words, you may not get 1-line to 1-line correspondence, but you would not be able to reduce the assembler any further. C can be extremely close to assembler in terms of efficiency.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  32. #32

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by moeur
    I don't know if this helps you or not, but here is one way you can create your C++ dll from VC++
    Well, I used to write C code for DOS a few years ago so I hope I can still work out the magic as you say, but finding my way in the VC++ IDE without any background and coming from VB is not so straighforward so your post came in like the answer to my prayers... Thanks a lot.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  33. #33

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Why is C faster than VB

    Quote Originally Posted by Dave Sell
    If you take a string parsing routine written in C, say a for loop using integer offsets to the base address of the string (array of bytes), and then decompile it to assembler, you will get about a 1-1 ratio of C code to assembler code. In other words, you may not get 1-line to 1-line correspondence, but you would not be able to reduce the assembler any further. C can be extremely close to assembler in terms of efficiency.
    Oh I see what you mean, C is as efficient as assembler. I thought you meant assembler was less efficient than C, silly me !!!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  34. #34
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles!

    In many cases a C compiler will produce faster code than you will produce writing in assembly language yourself. This is beacuse the compiler knows a lot of tricks that you don't. I've actually tested it.

  35. #35

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by moeur
    Also add a MyDll.def file to the C++ project
    Code:
    EXPORTS
     ProcessData
    If you need help translating the magic I can help too.
    This is what I've tried so far (I have made a few changes)
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    /************************************************************************************************************************
    declare Sub ProcessData Lib "MyDll.dll" (DataByte as Byte, Val12bit as Long, Max12bit as Long, ByVal BufferSize as Long) 
    *************************************************************************************************************************/
    __declspec( dllexport ) void _stdcall ProcessData (char DataByte[][3], short Val12bit[], long Max12bit,long BufferSize)
    
    {
    	int i;
    	int j;
    	
    	Max12bit=0;
    	for (i=0;i<=BufferSize;i++)
    		j=2*i;
    		Val12bit[j] = (DataByte[i][2] & 240) * 16 | DataByte[i][0];
            Val12bit[j + 1] = (DataByte[i][2] & 15) * 256 | DataByte[i][1];
            if (Max12bit < Val12bit[j])
    			Max12bit = Val12bit[j];
            if (Max12bit < Val12bit[j + 1])
    			Max12bit = Val12bit[j + 1];		
    	return;
    }
    But when I try to add a reference to the dll an error message shows up, probably I didn't add this MyDll.def file in the correct way: I just added a text file with the text you posted.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  36. #36
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles!

    You get an error from VB?
    What is the error?

    After creating the def file, add it to your project
    Project\Add to Project\Files
    Files of Type\Definition Files (.def)
    This should add the following to
    Project\Settings\Link\Project Options
    /def:".\MyDll.def"

  37. #37
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    It's been a while since I did this but if you want to expose a function in a DLL don't you need to include windows.h? As I see it there is no reason at all to include stdio.h or math.h since you don't use any functions in those libraries.

  38. #38
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles!

    if you want to expose a function in a DLL don't you need to include windows.h
    No
    there is no reason at all to include stdio.h or math.h since you don't use any functions in those libraries.
    This is true, but it doesn't hurt to include them since he may be adding code later that needs them. Or not...

  39. #39
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    Well, I doubt very much that he will need any functions from the stdio.h library since that's an old C library used for IO in the console world (which BTW is subsided by the iostrem.h in C++).

    Anyway thanks for the info about not needing the windows.h library to expose a Windows DLL function (I wanted to give you a rating for that, but I wasn't allowed to since I rated you earlier and I haven't rated enough people to give you a new one ), but I do still believe that windows.h is more valuable then stdio.h in the Windows world
    Last edited by Joacim Andersson; Apr 17th, 2005 at 07:08 PM.

  40. #40

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by moeur
    You get an error from VB?
    What is the error?

    After creating the def file, add it to your project
    Project\Add to Project\Files
    Files of Type\Definition Files (.def)
    This should add the following to
    Project\Settings\Link\Project Options
    /def:".\MyDll.def"
    I have to make sure what exactly the error was but at the moment I'm sitting in front of a computer where VC++ is not installed. I think it was something like "It was not possible to add a reference to this library" when I tried to add the reference in the VB project/references menu.
    As I said, I had to put the

    EXPORTS
    ProcessData

    lines in a text file. But when I tried to add it to the project there were no def files among the different types available (to be sure, I'd had a couple of beers before that) so I added it as a cpp code file.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width