Sign in to follow this  
Followers 0
cheney

Initial data pattern to buffer

6 posts in this topic

#1 ·  Posted (edited)

Hi all:

$nBytes = 1024

$hBuffer = DllStructCreate("ubyte buffer[" & $nBytes & "]")

$pBuffer = DllStructGetPtr($hBuffer)

.

How to fill data pattern 0x55AA or any data pattern to $hBuffe not with "for"?

What method is fast ?? call memset ??

Thanks for your answer :blink:

Edited by cheney

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$nBytes = 1024
$hBuffer = DllStructCreate("ubyte buffer[" & $nBytes & "]")
$pBuffer = DllStructGetPtr($hBuffer)


$word = BinaryToString("0x55AA")

$t = TimerInit()
Do ;create string
    $word &= $word & $word ;grows fast....
Until StringLen($word) >= $nBytes
$word = StringLeft($word, $nBytes) ;strip
DllStructSetData($hBuffer, 1, $word)
$m = TimerDiff($t)

;verify
MsgBox(0, "Filled $hbuffer in " & $m & " ms", DllStructGetData($hBuffer, 1))

Edited by AndyG

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

After flipping the high/low order of the bytes, this seems to be blazing fast:

DllCall("ntdll.dll", "none", "RtlFillMemoryUlong", "ptr", $pBuffer, "int", 1024, "ulong", 0xAA55AA55)
Edited by Spiff59

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

   :blink: now it is academically....i ran 10000 loops with each solution.... 

dll-call 250 ms     vs      string 165 ms

both equally fast....

But the bigger the struct, the faster the dll......i would prefer the dll 

Edited by AndyG

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I just went and looked on MSDN and found that call when I read the original post in here.

But I don't think I'd regard 165 vs 250 as equally fast.

I've never liked doing benchmarks via TimerInit/Diff, you can get some crazy results run-to-run, or weird fluctuations depending on the iterations. I just tried this:

#include <Array.au3>

$nBytes = 1024
$hBuffer = DllStructCreate("ubyte buffer[" & $nBytes & "]")
$pBuffer = DllStructGetPtr($hBuffer)

Global $loop, $Benchtable[5][4]
For $loop = 0 to 4
    $Benchtable[$loop][0] = 10 ^ ($loop + 1)
    Benchmark()
Next
_ArrayDisplay($Benchtable)
Exit

;===============================================================================
Func Benchmark()
    $t = TimerInit()
    For $x = 1 to $Benchtable[$loop][0]
        $word = BinaryToString("0x55AA") ; needs to be inside loop to be reset when doing multiple iterations for benchmark testing
        Do ;create string
            $word &= $word & $word ;grows fast....
        Until StringLen($word) >= $nBytes
        $word = StringLeft($word, $nBytes) ;strip
        ;   DllStructSetData($hBuffer, 1, $word)
    Next
    $Benchtable[$loop][1] = TimerDiff($t)

    $t = TimerInit()
    For $x = 1 to $Benchtable[$loop][0]
        $word = BinaryToString("0x55AA") ; statement here only to make the benchmark comparison fair
        DllCall( "ntdll.dll", "none", "RtlFillMemoryUlong", "ptr", $pBuffer, "int", 1024, "ulong", 0xAA55AA55)
    Next
    $Benchtable[$loop][2] = TimerDiff($t)

    $t = TimerInit()
    $dll = DllOpen("ntdll.dll") ; faster when called multiple times with this statement
    For $x = 1 to $Benchtable[$loop][0]
        DllCall($dll, "none", "RtlFillMemoryUlong", "ptr", $pBuffer, "int", 1024, "ulong", 0xAA55AA55)
    Next
    DllClose($dll)
    $Benchtable[$loop][3] = TimerDiff($t)
EndFunc

I wasn't sure how to fairly handle resetting the $Word variable... I put a reset of it in the second loop, and tuned the third loop for speed

typo

Edited by Spiff59

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thank you all for the answering :blink:

Edited by cheney

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0