AutoHotkey Community

It is currently May 26th, 2012, 12:34 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 131 posts ]  Go to page Previous  1 ... 5, 6, 7, 8, 9  Next
Author Message
 Post subject:
PostPosted: February 1st, 2007, 8:05 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776

Dear Laszlo & Titan :)

Laszlo wrote:
This is the shortest code I know of


:D I have two versions now. One takes 5 lines and damn slow.
The other one is a 6 liner, but somewhat faster ..

toHexing a 190kb file in my 1.4Ghz Sempron takes:

Laszlo version: 730ms
Titan version: 710ms

My 6 Liner: 1320ms
My 5 Liner: 1930ms

Shortest code need not be the fastest .... I am learning

:( :( :(


Here is my version:

Code:
toHex( ByRef V, ByRef H, dataSz=0 )  {
P := ( &V-1 ), VarSetCapacity( H,(dataSz*2) ), Hex := "123456789ABCDEF0"
Loop, % dataSz ? dataSz : VarSetCapacity( V )
     H  .=  SubStr(Hex, (*++P >> 4), 1) . SubStr(Hex, (*P & 15), 1)
}


Takes more than twice the time than Titans fastest version, but the technique used has been helpful for me in my ChooseColor function.

:)

Edit: It requires an additional VarSetCapacity !


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 1st, 2007, 9:49 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Since AHK allows multiple commands in the same line, the number of lines are not the best measure of the code size. One could count the number of instructions, but that is misleading, too. For example func1(func2(func3,x),y),z) is a single instruction, but it performs 3 function calls.

Skan, your code has the dangerous expression SubStr(Hex, (*++P >> 4), 1) . SubStr(Hex, (*P & 15), 1). There is no guaranty that a future version of AHK will execute ++P before the reference to P in the second SubStr call. It is unlikely, that it will ever be the other way, but you are relying on an undocumented feature of AHK, which might change without a warning. (Documented features change, too, but they are always listed in the change log.)

If you plan to do large hex conversions, you can go one step further, and setup a full byte substitution table outside of the hex conversion function. It could give a little speedup.

Yet another idea was to use the dynamic variable references of AHK, instead of SubStr. It would be interesting to know, which is faster.
Code:
SetFormat Integer, Hex
Loop 256
   Hex .= SubStr(A_Index+255,4,2)           ; byte table
SetFormat Integer, Dec
Loop 255
   H_%A_Index% := SubStr(Hex,2*A_Index+1,2) ; H_0, H_1,...H_255 = 00, 01,...ff
H_0 = 00

toHex1(s:="@AB" chr(1), h, 5)
MsgBox %h%
toHex2(s:="@AB" chr(1), h, 5)
MsgBox %h%
toHex3(s:="@AB" chr(1), h, 5)
MsgBox %h%

toHex0(ByRef b, l = 0) {                    ; Titan'
   f := A_FormatInteger
   p := &b
   SetFormat, Integer, h
   Loop % l ? l : VarSetCapacity(b)
      h .= *p++
   SetFormat Integer, %f%
   Return RegExReplace(RegExReplace(h, "0x(.)(?=0x|$)", "0$1"), "0x")
}

toHex1( ByRef V, ByRef H, dataSz=0)  {      ; Skan'
   P := &V
   VarSetCapacity( H, dataSz*2 )
   Hex = 123456789ABCDEF0
   Loop % dataSz ? dataSz : VarSetCapacity(V)
      H .=  SubStr(Hex, *P >> 4, 1) . SubStr(Hex, *P++ & 15, 1) ; Danger!
}

toHex2( ByRef V, ByRef H, dataSz=0)  {      ; Skan**2
   Global Hex
   P := &V
   VarSetCapacity( H, dataSz*2 )
   Loop % dataSz ? dataSz : VarSetCapacity(V)
      H .=  SubStr(Hex, *P++*2+1, 2)
}

toHex3( ByRef V, ByRef H, dataSz=0)  {      ; Dynamic vars
   P := &V
   VarSetCapacity( H, dataSz*2 )
   Loop % dataSz ? dataSz : VarSetCapacity(V)
      i := *P++, H .=  H_%i%
}

Most of the running time is spent in the loop, so the fastest algorithm will always be with the simplest instruction in the body of the loop. It is Titan's "h .= *p++", or "h .= *(p+A_Index)" with a prior "p := &V-1". This later version could be slightly faster, because it does not store the incremented value of p.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2007, 7:16 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Laszlo wrote:
Since AHK allows multiple commands in the same line, the number of lines are not the best measure of the code size. One could count the number of instructions, but that is misleading, too. For example func1(func2(func3,x),y),z) is a single instruction, but it performs 3 function calls.


True .. I do not claim my code is shorter.

Laszlo wrote:
Skan, your code has the dangerous expression SubStr(Hex, (*++P >> 4), 1) . SubStr(Hex, (*P & 15), 1). There is no guaranty that a future version of AHK will execute ++P before the reference to P in the second SubStr call . It is unlikely, that it will ever be the other way, but you are relying on an undocumented feature of AHK, which might change without a warning. (Documented features change, too, but they are always listed in the change log.)


Yes Sir! .. I am aware of it after reading your informative posts in the topic Lite Decimal to Binary (and vice versa) Conversion Functions. But I had written toHex() and ChooseColor() prior to it ( for a tutorial - which I have dropped ). My version of the toHex() is pretty slower for large hex conversions and I would happily use Titans' version. But ChooseColor() has been written for my ColorPicker GENIE, and I will be quick enough to change it if the code breaks with a future AHK version.

Thanks for showing the right way and for rectifying my version of toHex().

Laszlo wrote:
Most of the running time is spent in the loop, so the fastest algorithm will always be with the simplest instruction in the body of the loop. It is Titan's "h .= *p++", or "h .= *(p+A_Index)" with a prior "p := &V-1". This later version could be slightly faster, because it does not store the incremented value of p.


Yes Sir .. I am learning. Many Thanks again.

Regards, :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2007, 7:25 pm 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
Quote:
func1(func2(func3,x),y),z) is a single instruction, but it performs 3 function calls


Actually it is 2 function calls and a syntax error... :shock:
I would expect more from you.. :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2007, 7:45 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
ahklerner wrote:
I would expect more from you.
There was an old professor in my university, who said when confronted with a typo, that it was intentional. Checking if you pay attention. I am glad you did! :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2007, 7:55 pm 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
:D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2007, 7:58 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
@Laszlo: I did not notice it.. :( I have the habit of taking your code as granted. :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 8th, 2008, 10:59 pm 
ExtractInteger() has been removed from AutoHotkey Help - so this link shows the page but nothing re ExtractInteger()
this has happened without word from Chris - nothing about it in the Changelog
does anyone know why?
where can i find ExtractInteger() ?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 8th, 2008, 11:09 pm 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3329
Location: Simi Valley, CA
Anonymous wrote:
where can i find ExtractInteger() ?

That function has been made obsolete by NumGet().

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 14th, 2008, 11:46 pm 
[VxE] - thank you very much

i now find it in the changlog:

Quote:
Added NumGet() and NumPut(), which retrieve/store binary numbers with much greater speed than Extract/InsertInteger.


no wonder that i couldn't find it with a search for ExtractInteger

if Chris will only change Extract/InsertInteger to ExtractInteger & InsertInteger then it can be found with a search (but at least this post will turn up)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 15th, 2008, 9:13 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
Anonymous wrote:
no wonder that i couldn't find it with a search for ExtractInteger
I guess you didn't try the help file, which has an item in the index "ExtractInteger -> NumGet()"...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 8:28 pm 
SKAN wrote:
Quote:
How to manipulate Binary data with Pointers ?


I didn't even know AHK had pointers... this thread is epic in an extreme way.

THANK YOU


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 8:31 pm 
Offline

Joined: July 6th, 2009, 9:58 pm
Posts: 678
On a related note C is very powerful in pointers but the data type requirements are annoying... I'm not sure how AHK handles those inherent restrictions but it's pretty epic to just be able to define an actual string and then run pointers through it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Do these techniques potentially allow AHK to read from crazy data structures like MP3 or what have you?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: ea2apaza
PostPosted: November 15th, 2010, 8:39 pm 
SKAN wrote:
How to search and replace a text string present in a Binary file ?

I have seen repeated requests for changing the ahk_class for compiled scripts' GUI.
Credit: The technique of string replacement in AutoHotkeySC.bin with a Hex editor was posted by Serenity here : Compiled scripts Window class

I present my pure AHK version of the said technique. It is neither very fast, nor too slow, but should suffice for quick toggling of the ahk_class:

Code:
; AutoHotkeySC.bin Patcher / Written by A.N.Suresh Kumar AKA "Goyyah", 24-Nov-2006.

SetBatchLines, -1

ahk_class_exi := "AutoHotkeyGUI" ; Hopefully not been changed already by the script.
ahk_class_new := "AHK-GUI      " ; Pad enough spaces to match length of above var.
StringLeft, ahk_class_new, ahk_class_new, 15     ; Make sure of the writable length.

AHKSC :=  RegExReplace(A_AhkPath, "(.*)\\.*$", "$1") . "\Compiler\AutoHotkeySC.Bin"
IfNotExist, %AHKSC%, ExitApp     ; Make sure that the file exists.
FileGetSize, binDataSz, %AHKSC%  ; StrLen() does not work for Binary data
FileRead   , binData  , %AHKSC%  ; Read the whole file into a variable

Loop, %binDataSz%                       
  If ( *(&binData+(A_Index-1)) = 0 )
     DllCall( "RtlFillMemory", UInt,&binData+(A_Index-1), Int,1, UChar,32 )

; The above loop tinkers the binData by replacing Null Characters with Spaces
                                 
StrOffset := InStr( binData, ahk_class_exi ) - 1
If (StrOffset < 0) {
   MsgBox, 16, AutoHotkeySC.bin Patcher, String not found !? :: %ahk_class_exi%
   ExitApp     
                   }     

; Following are obsolete 16Bit file IO functions that gets the patchwork done!

hFile := DllCall( "_lopen", Str,AHKSC, Int,0x2 )
DllCall( "_llseek", Int,hFile, Int,StrOffset, Int,0 )
DllCall( "_lwrite", Int,hFile, Int, &ahk_class_new, Int,StrLen(ahk_class_new) )
DllCall( "_lclose", Int,hFile )

MsgBox, 64, AutoHotkeySC.bin Patcher, Successfully Patched :: %ahk_class_new%, 10


That is it :!: :?:

PS: I am not satisfied. I have explained only about UCHAR that occupies a single memory byte. There are WORD ( 2 byte ) and DWORD ( 4 byte ) that stores 32 bit integers..... But, this post is already too long :( . Maybe later! :)

Regards, :)

Hi Skan,
you already mentioned, that there may be WORD and DWORD-Characters. Now with AHK_L it has happened. With the latest AHK_L-version of Autohotkey.bin the Autohotkey.bin patcher isn't running any more (Errormsg String not found !?). I think, it's only a small change for you, but a giant step for me. So would it be possible to adapt yout script to AHK_L and UTF-8?

Thanks in advance.


Report this post
Top
  
Reply with quote  
 Post subject: Re: ea2apaza
PostPosted: November 16th, 2010, 2:11 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
ruespe* wrote:
would it be possible to adapt yout script to AHK_L


Code:
; AutoHotkeySC.bin String Patcher for AHK_L 32b Unicode             / by SKAN 16-Nov-2010.

PathToAHKSC := "E:\Lexikos\AutoHotkeySC.bin"
     StrOld := "AutoHotkeyGUI"
     StrNew := "MyGUI"

FileGetSize, Sz, %PathToAHKSC%
FileRead, Bin, *c %PathToAHKSC%

nLen   := StrLen( StrOld ) * 2
VarSetCapacity( StrRep, nLen, 0 )
StringLeft, StrRep, StrNew, %nLen%

If ( ( Off := InBuf( &Bin,Sz, &StrOld,nLen ) ) < 0 ) {
 MsgBox, 16, AutoHotkeySC.bin Patcher, String not found !? :: %StrOld%
 ExitApp
}

hFile := DllCall( "_lopen", AStr,PathToAHKSC, Int,0x2 )
DllCall( "_llseek", UInt,hFile, UInt,Off, Int,0 )
DllCall( "_lwrite", UInt,hFile, UInt,&StrRep, UInt,nLen )
DllCall( "_lclose", UInt,hFile )

Return ;                                                 // end of auto-execute section //


InBuf( hayP,hayS, neeP,neeS, sOff=0 ) {    ; slightly altered version of InBuf() by wOxxOm
 Static InBuf      ; Original version available @ www.autohotkey.com/forum/topic25925.html
 If ( ! VarSetCapacity( InBuf ))  {
     FiH =
     ( LTrim Join
       530CEC83E58955|5D8B9C57565251|C28E0F00FB8314|8B104D8B000000|41D929C1291845|8B000000
       B18E0F|0C758BC701087D|2A744BACFCC031|4B36744B2D744B|AD933F754B1474|008B850FAEF293|E
       BF4751F390000|7F75AEF2AD4E75|68EBF775FF4739|8A62EB7475AEF2|27386C75AEF226|AD669356E
       BF875|39665E75AEF293|434E47EBF7751F|C1DA89FC7589AD|E283F45D8902EB|87DF87F8558903|AE
       F2CA87FB87D1|F775FF47393775|03C783CA89FB89|85F44D8BFC758B|DE75A7F30474C9|0474C985F8
       4D8B|4FDF89D375A6F3|5F9D08452BF889|14C2C95B595A5E|F0EBD0F7C03100
     )
 VarSetCapacity( InBuf,224,0 )
 Loop, Parse, FiH, |
   NumPut( "0x" A_LoopField, InBuf,(7*(A_Index-1)), "Int64" )
 }
Return DllCall( &InBuf, UInt,hayP, UInt,neeP, UInt,hayS, UInt,neeS, UInt,sOff )
}

; "AutoHotkeyGUI" in Unicode hex: 4100750074006F0048006F0074006B0065007900470055004900



Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 131 posts ]  Go to page Previous  1 ... 5, 6, 7, 8, 9  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group