[RESOLVED] Processing Time For PVOID Into Byte Array Conversion
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: [RESOLVED] Processing Time For PVOID Into Byte Array Conversion

  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    147

    [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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by saraswathisrinath View Post
    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

  3. #3
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by saraswathisrinath View Post
    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

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,886

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by saraswathisrinath View Post
    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.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,886

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by Paul McKenzie View Post
    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 ;-)

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,378

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Nov 2011
    Location
    India
    Posts
    147

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by Paul McKenzie View Post
    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.

    Quote Originally Posted by Paul McKenzie View Post
    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.

    Quote Originally Posted by OReubens View Post
    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);		
        }
    Quote Originally Posted by 2kaud View Post
    First, your code has a problem
    Sorry Mr.2kaud, that's my mistake. But I'm correctly used in my project.


    Quote Originally Posted by 2kaud View Post
    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

  8. #8
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by saraswathisrinath View Post
    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.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by saraswathisrinath View Post
    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.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    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.

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,378

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,886

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by saraswathisrinath View Post
    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;

  13. #13
    Join Date
    Nov 2011
    Location
    India
    Posts
    147

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by Paul McKenzie View Post
    it could be characters, floating point, integers, etc.
    It could be byte.

    Quote Originally Posted by Paul McKenzie View Post
    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?
    Quote Originally Posted by GCDEF View Post
    all you need to do is change your pointer type.
    Yes. I agree . How is possible?

    Quote Originally Posted by 2kaud View Post
    which as expected shows that using memcpy is the fastest and array indexing the slowest.
    Ok I will use and come back.
    Quote Originally Posted by OReubens View Post
    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

  14. #14
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,378

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Processing Time For PVOID Into Byte Array Conversion

    Quote Originally Posted by saraswathisrinath View Post
    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.

Page 1 of 2 12 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center