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.
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.
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.
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 !!! :blush:
Re: Nibbles! [Happy with your answers]
I don't think you would gain very much by moving the graphic plotting to a C DLL. The built in VB graphic functions are extremly slow but you can use all GDI functions from VB instead. They won't run much faster in a C application compared to a VB application, all you add is one more API call (the one to your own DLL).
Re: Nibbles: still need help
First of all there is an error in your declaration, should be
Code:
__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;
}
Show me how you're dimensioning your arrays and calling the routine from VB.
Should be
VB Code:
Private Declare Sub ProcessData Lib "MyDll.dll" ( _
DataByte As Byte, _
Val12bit As Integer, _
Max12bit As Long, _
ByVal BufferSize As Long _
)
and something like
VB Code:
Dim DataByte(0 To 3, 0 To 100) As Byte
Dim Val12bit(0 To 100) As Integer
Dim Max12bit As Long
Dim BufferSize As Long
BufferSize = 100
And call it like
VB Code:
Call ProcessData(DataByte(0, 0), Val12bit(0), Max12bit, BufferSize)
2 Attachment(s)
Re: Nibbles: still need help
Quote:
Originally Posted by moeur
Show me how you're dimensioning your arrays and calling the routine from VB.
VB Code:
'These are my declarations:
Private Declare Sub ProcessData Lib "Mydll.dll" (DataByte As Byte, _
Val12bit As Integer, _
Max12bit As Integer, _
ByVal BufferSize As Long)
'Data declarations:
Dim Max12bit As Integer
Dim DiskData() As Byte
Dim Val12bit() As Integer
'Calling of dll code:
Dim BufferSize As Long
'dsize is the size of the data area in the file
'i.e. dsize = filesize - headersize
BufferSize = 2 * dSize \ 3
ReDim Val12bit(BufferSize )
Call ProcessData(DiskData(0, 0), Val12bit(0), Max12bit, BufferSize)
This however produces an error (see figure):
Re: Nibbles: still need help
Shouldn't it be?
VB Code:
ReDim Val12bit(2*BufferSize )
Since in your C code,
i goes from 0 to Buffersize-1 and j=2*i
Re: Nibbles: still need help
Besides the fact pointed out by moeur above there is also a logical error in your C code. The for loop should use the curly brackets...
Code:
for (i=0; i<BufferSize; i++) {
j=2*i;
//... the rest of the code
}
return;