-
March 12th, 2013, 02:47 AM
#1
[RESOLVED] Processing Time For PVOID Into Byte Array Conversion
Hi,
In my project I'm using PVOID pointer to store real time data.
After getting this data I will convert the data into byte array, like below
Code:
byte *bPoint = NULL;
PVOID pvData;
byte TempArr[1024];
bPoint = (byte*) pvData;
for(int i=0;i<1024;i++)
TempArr[i] = (byte) (*bPoint + i);
Processing time for the above code takes 9500 to 9900 microseconds (Used QueryPerformanceCounter).
Code:
TempArr[0] = ((BYTE*) pvData) [0];
This code takes 1100 to 1200 microseconds.
My doubt is, The processing time of PVOID data into byte array conversion takes time like above?
Or any other easy way(PVOID data into byte array conversion) to reduce the processing time?
Please clear me.
Regards,
SaraswathiSrinath
-
March 12th, 2013, 04:15 AM
#2
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by saraswathisrinath
Hi,
In my project I'm using PVOID pointer to store real time data.
After getting this data I will convert the data into byte array, like below
Why aren't you using memcpy()?
Regards,
Paul McKenzie
-
March 12th, 2013, 04:23 AM
#3
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by saraswathisrinath
After getting this data I will convert the data into byte array, like below
Also, you are not converting anything. What you're doing is copying from one memory block to another. Mass copying is done efficiently using memcpy(), and not by writing a for-loop.
On the other hand, converting implies you're using some sort of algorithm or function to transform from one type to another, and you're not doing that here.
Regards,
Paul McKenzie
-
March 12th, 2013, 07:49 AM
#4
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by saraswathisrinath
Processing time for the above code takes[B] 9500 to 9900 microseconds (Used QueryPerformanceCounter).
9500 microseconds is 9.5 milliseconds, or 0.0095seconds... copying 1024 bytes does not take that long, not even on an original IBM XT would it take that long.
Note that QueryPerformanceCounter does NOT return microseconds.
What it returns is a computer dependant value that indicates a discrete time interval for that specific computer. You need to use QueryPerformanceFrequency() to convert the results from QueryPerformanceCounter() into actual second-based values.
-
March 12th, 2013, 07:52 AM
#5
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by Paul McKenzie
Mass copying is done efficiently using memcpy(), and not by writing a for-loop.
Not necessarily. A smart compiler optimizer will see the sequence and convert it to a memcpy on it's own, or potentially even tailor a specific optimal bit of copying code. Don't assume the compiler will simply make a loop and copy bytes one by one.
Then again, don't assume the compiler WILL make this optimisation either ;-)
-
March 12th, 2013, 08:34 AM
#6
Re: Processing Time For PVOID Into Byte Array Conversion
Or any other easy way(PVOID data into byte array conversion) to reduce the processing time?
First, your code has a problem
Code:
TempArr[i] = (byte) (*bPoint + i);
should be
Code:
TempArr[i] = (byte) *(bPoint + i);
to correctly index memory pointed to by bPoint
Using an array index within the loop is the most expensive way of performing the copy. On my computer using VS fully optimised for speed, this gives an average of 4140 (using QueryPerformanceCounter)
Code:
PVOID pvData = (PVOID)data;
byte *bPoint = (byte*) pvData;
byte TempArr[1024];
byte *bTo = TempArr;
for(int i = 0; i < 1024; i++)
TempArr[i] = (byte) *(bPoint + i);
Just changing the assignment to use pointers rather than index reduces the value to average 880
Code:
PVOID pvData = (PVOID)data;
byte *bPoint = (byte*) pvData;
byte TempArr[1024];
byte *bTo = TempArr;
for(int i = 0; i < 1024; i++)
*bTo++ = *bPoint++;
And using memcpy reduces this further to average 850
Code:
PVOID pvData = (PVOID)data;
byte *bPoint = (byte*) pvData;
byte TempArr[1024];
byte *bTo = TempArr;
memcpy(bTo, bPoint, 1024);
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 12th, 2013, 11:42 PM
#7
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by Paul McKenzie
Why aren't you using memcpy()?
I have PVOID data only. But I like to draw real time image using byte array. So I convert this into byte array. I get that method from net and I used. After that only I noticed, my program getting delay for this data conversion.
Originally Posted by Paul McKenzie
On the other hand, converting implies you're using some sort of algorithm or function to transform from one type to another, and you're not doing that here.
I never used before any algorithm for data conversion. Please give any reference, after that I will try in my project.
Originally Posted by OReubens
You need to use QueryPerformanceFrequency() to convert the results from QueryPerformanceCounter() into actual second-based values.
I'm processing in the speed of Milliseconds. If I return second based means it must return 0 only.
Code:
LARGE_INTEGER m_lnStart;
LARGE_INTEGER m_lnEnd;
LARGE_INTEGER m_lnFreq;
QueryPerformanceFrequency(&m_lnFreq);//Retrieves the frequency of the high-resolution performance counter
QueryPerformanceCounter( &m_lnStart);//Retrieves the current value of the high-resolution performance counter
byte *bPoint = (byte*)pvData;
for(int i = 0; i < 1024; i++)
TempArr[i] = (byte)(*(bPoint + i));
QueryPerformanceCounter( &m_lnEnd );
__int64 nDiff(0) ;
double dMircoSecond( 0 ) ;
if( m_lnStart.QuadPart !=0 && m_lnEnd.QuadPart != 0)
{
dMircoSecond = (double)(m_lnFreq.QuadPart/1000000);
nDiff = m_lnEnd.QuadPart-m_lnStart.QuadPart ;
dMircoSecond = nDiff/dMircoSecond;
CString str;
str.Format("%d", nDiff);
GetDlgItem(IDC_TIMEDIF)->SetWindowText(str);
}
Originally Posted by 2kaud
First, your code has a problem
Sorry Mr.2kaud, that's my mistake. But I'm correctly used in my project.
Originally Posted by 2kaud
And using memcpy reduces this further to average 850
850 microseconds or Milliseconds?
My requirement is, I'm getting PVOID data continuously, using that i convert into 2D byte array & draw an image.Important note is these process are done by minimum 1 millisecond. If process time exceed then i miss some data in real time.
Regards,
SaraswathiSrinath
-
March 13th, 2013, 05:43 AM
#8
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by saraswathisrinath
I have PVOID data only.
There is no such thing as "PVOID data".
You have a pointer, and starting at that pointer is data in one contiguous block. How you interpret that contiguous block could be anything -- it could be characters, floating point, integers, etc. But just because the data is interpreted differently doesn't change the data. The data at that memory location stays exactly the same.
That's why I'm stating to you that you are not converting anything. You're interpreting the data differently, but you are not converting. You're simply copying the same data from one memory block to another memory block, and then at the destination block, you're interpreting that data as pointing to a series of "bytes".
An example of a real conversion would be going from ANSI to Unicode, or from binary to hexadecimal, or going from little-endian to big-endian where you need to actually change the data by swapping bytes, etc.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 13th, 2013 at 05:48 AM.
-
March 13th, 2013, 05:52 AM
#9
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by saraswathisrinath
I never used before any algorithm for data conversion. Please give any reference, after that I will try in my project.
I'm not saying to use an algorithm. What I'm saying is that what you posted is not conversion. It is copying data from one memory location to another. I don't know how I can put it more simply.
So your goal is to copy contiguous data from one memory location to another -- that is what everyone responding to you is giving you advice on. The fastest way is to write the copying in assembly language using the fastest mass-copying assembly instructions for your CPU. You can't get any faster than that, regardless of your requirements.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 13th, 2013 at 06:02 AM.
-
March 13th, 2013, 07:38 AM
#10
Re: Processing Time For PVOID Into Byte Array Conversion
Is the only reason you're copying it because you think you're converting it? If so, all you need to do is change your pointer type.
-
March 13th, 2013, 08:29 AM
#11
Re: Processing Time For PVOID Into Byte Array Conversion
850 microseconds or Milliseconds?
The numbers are the difference between two calls to QueryPerformanceCounter. For the sake of the comparisons I did it doesn't really matter what are the units of measurement - just the relative numbers - which as expected shows that using memcpy is the fastest and array indexing the slowest.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 13th, 2013, 08:51 AM
#12
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by saraswathisrinath
Code:
dMircoSecond = (double)(m_lnFreq.QuadPart/1000000);
This is wrong.
If you have to go this somewhat awkward way about it, you need to do a floating point divide. you're here doing an integer divide, throwing away the decimal part and storing only the integer result into dMircoSecond.
This will work and give you a better result.
Code:
dMircoSecond = (double)m_lnFreq.QuadPart/1000000;
-
March 16th, 2013, 12:30 AM
#13
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by Paul McKenzie
it could be characters, floating point, integers, etc.
It could be byte.
Originally Posted by Paul McKenzie
That's why I'm stating to you that you are not converting anything. You're interpreting the data differently, but you are not converting. You're simply copying the same data from one memory block to another memory block, and then at the destination block, you're interpreting that data as pointing to a series of "bytes".
I clearly understood. Thanks Mr.Paul. How can I copy the data from one memory block to another. That is, I will copy the PVOID pointer data into byte *pointer like below.
Code:
PVOID pvData = (PVOID)data;
byte *bPoint = (byte*) pvData;
byte TempArr[1024];
byte *bTo = TempArr;
memcpy(bTo, bPoint, 1024);
Then I will copy the byte pointer data into byte array.
Am I correct?
Originally Posted by GCDEF
all you need to do is change your pointer type.
Yes. I agree . How is possible?
Originally Posted by 2kaud
which as expected shows that using memcpy is the fastest and array indexing the slowest.
Ok I will use and come back.
Originally Posted by OReubens
This is wrong. you're here doing an integer divide, throwing away the decimal part and storing only the integer result into dMircoSecond.
I will change my code.
Regards,
SaraswathiSrinath
-
March 16th, 2013, 07:06 AM
#14
Re: Processing Time For PVOID Into Byte Array Conversion
I have PVOID data only. But I like to draw real time image using byte array.
If all you need is a byte pointer to the data for drawing purposes, then replace
Code:
PVOID pvData = (PVOID)data;
byte *bPoint = (byte*) pvData;
byte TempArr[1024];
byte *bTo = TempArr;
memcpy(bTo, bPoint, 1024);
with
Code:
byte *TempArr = (byte*)data;
and then just draw from TempArr as currently.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 16th, 2013, 10:36 AM
#15
Re: Processing Time For PVOID Into Byte Array Conversion
Originally Posted by saraswathisrinath
I clearly understood. Thanks Mr.Paul. How can I copy the data from one memory block to another. That is, I will copy the PVOID pointer data into byte *pointer like below.
First, it isn't clearly understood as to why you want to copy the data.
My requirement is, I'm getting PVOID data continuously, using that i convert into 2D byte array & draw an image.Important note is these process are done by minimum 1 millisecond. If process time exceed then i miss some data in real time.
So you're getting this data "continuously", but you don't mention if that memory block is being continuously overwritten in an asynchronous manner. Real-time programming doesn't just mean you get the data fast -- where is the code to ensure that the block of data is not overwritten while you're copying or processing the data? Where is the queue or queue-like implementation to ensure that whatever data you get, it goes on your queue so that nothing is missed or overwritten?
Yes, processing data quickly is a requirement so that you don't overload the queue, but at the same time, you have to ensure that you don't miss any data coming in. You don't do that by "coding the fastest routine" and hoping that the routine processes all the data before some real-time function refreshes the buffer with new data. That is a naive and wrong approach. You have to use the proper data structures (probably a queue), that caches the data that comes in so you don't miss anything.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 16th, 2013 at 10:50 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|