Jump to content

Recommended Posts

Posted
  On 5/10/2021 at 4:29 PM, Musashi said:

The solutions from @JockoDundee, @Nine and @Chimp only work if you specify exactly 8 bits.

Expand  

Not in my case, it is based on StringLen which could be anything.  Moreover it allows any bases from 2 to 16.  Take a closer look. :ermm:

Posted

 

  On 5/10/2021 at 5:31 PM, JockoDundee said:

Free your mind from conceptual limitations, and all sorts of solutions will appear :)

Expand  

lol, I do understand that arrays are said to be significantly faster. such as how you did it just in general programming. 

It's significantly slower, but is doing more too. I think this maybe the most practical approach.  

Global $iIter = 100000, $sOut, $t = TimerInit()

Global Enum $0000, $0001, $0010, $0011, $0100, $0101, $0110, $0111, $1000, $1001
Global Const $1010 = "A", $1011 = "B", $1100 = "C", $1101 = "D", $1110 = "E", $1111 = "F", $cow = "mooo"
Global Const $dataStream = "1110111110101101010011010100"


For $n = 1 To $iIter
    Global $sbyte = $dataStream
    Local $bArray[StringLen ( $sbyte )/4+1]
    $bArray[0] = (StringLen ( $sbyte )/4)
    for $z = 1 To $bArray[0]
    $bArray[$z] = Eval(StringLeft($sbyte, 4))
    $sbyte = StringTrimLeft ( $sbyte, 4 )
    Next
Next

$t = TimerDiff($t)

For $z = 1 To $bArray[0]
ConsoleWrite(" Check: " & $bArray[$z] & " Dec:" &Dec($bArray[$z])& " " &@CRLF )
Next

ConsoleWrite($iIter & " Iterations in " & $t / 1000 & " Seconds" & @CRLF & @CRLF)

 

Posted

To be clear to those who bemoan the inflexibility of my solution, understand that the gloves came off the moment the OP said:

  Quote

Considering speed as a factor...
Expand  

At that point, a version which provided a 2x speed up over the best effort so far, was entirely on the mark :)

 

Code hard, but don’t hard code...

Posted
  On 5/10/2021 at 4:29 PM, Musashi said:

Here, just for comparison, are the runtime results on my stone-age PC

Expand  

Can you run again?  The relative order doesn't match mine:

Based on code from: JockoDundee
 Check: AA 170
 100000 Iterations in 0.2239925 Seconds

Based on code from: Chimp
 Check: AA 170
 100000 Iterations in 0.2703542 Seconds

Based on code from: jhcd
 Check: 00000000000000AA 170
 100000 Iterations in 0.6339325 Seconds

Based on code from: nine
 Check: 000000AA 170
 100000 Iterations in 1.1458971 Seconds

Based on code from: Musashi
 Check: 000000AA 170
 100000 Iterations in 1.4233144 Seconds

Its hard to imagine Eval's being quicker than direct array access.

Code hard, but don’t hard code...

Posted
  On 5/10/2021 at 5:04 PM, Nine said:

Not in my case, it is based on StringLen which could be anything.  Moreover it allows any bases from 2 to 16.  Take a closer look. :ermm:

Expand  

You are correct, please accept my heartfelt apologies ;).

I used the code that Jocko attached in his runtime tests :

Local $sBasedOn="nine", $iIter=100000, $sOut, $t=TimerInit()
Local $input="10101010", $bin, $ival, $iLen=8, $base=2

For $n=1 To $iIter
  $iVal = 0

  For $i = 0 to $iLen - 1
    $iVal += Dec(StringMid($input, $iLen - $i, 1)) * ($base^$i)
  Next

  $sOut=Hex(Int($iVal)) &" "&  Int($iVal)

Next

$t=TimerDiff($t)

ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)

(there you have to enter 8 bits)

Your original code works flawlessly.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted
  On 5/10/2021 at 6:02 PM, Musashi said:

I used the code that Jocko attached in his runtime tests :

Expand  

I modified his code to give it the best  result possible speedwise.  No offence was intended.  I also mentioned this when I did it.

Code hard, but don’t hard code...

Posted
  On 5/10/2021 at 5:43 PM, JockoDundee said:

... to those who bemoan the inflexibility of my solution ...

[...]

I modified his code to give it the best  result possible speedwise.  No offence was intended.  I also mentioned this when I did it.

Expand  

In advance :
There was never any intention to judge your solution as inflexible or to consider your code customization as an offense. You should know me well enough by now ;).  

All solutions fulfill the original requirement of @major_lee !

I just wanted to point out, that some solutions also take variable bit lengths into consideration (no more and no less).

  On 5/10/2021 at 6:02 PM, JockoDundee said:

Can you run again?  The relative order doesn't match mine. [...] Its hard to imagine Eval's being quicker than direct array access.

Expand  

I will do that right after dinner.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted
  On 5/10/2021 at 6:31 PM, Musashi said:

There was never any intention to judge your solution as inflexible or to consider your code customization as an offense. You should know me well enough by now ;).  

Expand  

yes of course.  :) 
If I sounded defensive it’s just because I don’t take lightly modifying others code, but in this case I felt if I didn’t, it would be unfair.

Code hard, but don’t hard code...

Posted
  On 5/10/2021 at 6:02 PM, JockoDundee said:

Can you run again?  The relative order doesn't match mine. Its hard to imagine Eval's being quicker than direct array access.

Expand  

Here are the results :

I used the current code from @Chimp and the one you attached to your post. 3 runs each, with 100,000 and 1,000,000 iterations.

  Reveal hidden contents

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted (edited)

@JockoDundee :

From your attached file JD.au3 :

[...]
$arr[1011]="E" 
[...]

Just a typo : 1110 has to be used for E

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted
  On 5/10/2021 at 7:35 PM, Musashi said:

I used the current code from @Chimp and the one you attached to your post. 3 runs each, with 100,000 and 1,000,000 iterations.

Expand  

Thanks!  and good catch on the ‘E’.   
I reran as well, and still showing the opposite by about 10%. 

Though, looking at my code, I realize there is an implied conversion from string to int that I didn’t see before.  are you compiling 64-bit or 32-bit?  any other flags?

@Chimp, did you see any performance difference between the two on your machine?

btw, another version I tried initially was with 16 functions and Execute, though it was pretty slow.

 

Code hard, but don’t hard code...

Posted (edited)

Here are my result with three same baseline test.

  Quote

Based on code from: nine
 Check: 000000AA 170 
 1000000 Iterations in 18.0934664 Seconds
Based on code from: nine
 Check: 000000AA 170 
 1000000 Iterations in 17.3470087 Seconds
Based on code from: nine
 Check: 000000AA 170 
 1000000 Iterations in 17.2345381 Seconds

Expand  
Local $sBasedOn="nine", $iIter=1000000, $sOut, $t=TimerInit()
Local $input="10101010", $bin, $ival, $iLen=8, $base=2

For $n=1 To $iIter
  $iVal = 0

  For $i = 0 to $iLen - 1
    $iVal += Dec(StringMid($input, $iLen - $i, 1)) * ($base^$i)
  Next

  $sOut=Hex(Int($iVal)) &" "&  Int($iVal)

Next

$t=TimerDiff($t)

ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)

 

  Quote

Based on code from: jhcd
 Check: 00000000000000AA 170 
 1000000 Iterations in 12.155069 Seconds
Based on code from: jhcd
 Check: 00000000000000AA 170 
 1000000 Iterations in 11.0118714 Seconds
Based on code from: jhcd
 Check: 00000000000000AA 170 
 1000000 Iterations in 9.2977334 Seconds

Expand  
Local $sBasedOn="jhcd", $iIter=1000000, $sOut, $t=TimerInit()

Local $input="10101010", $bin, $aDll=DllOpen("msvcrt.dll")

For $n=1 To $iIter

  $bin=DllCall($aDll, "int64:cdecl", "_wcstoi64", "wstr", $input, "ptr*", 0, "int", 2)[0]
  $sOut=Hex($bin) &" "& $bin

Next
$t=TimerDiff($t)
ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)
  Quote

Based on code from: Musashi
 Check: 000000AA 170 
 1000000 Iterations in 21.2172401 Seconds
Based on code from: Musashi
 Check: 000000AA 170 
 1000000 Iterations in 21.3587079 Seconds
Based on code from: Musashi
 Check: 000000AA 170 
 1000000 Iterations in 20.9960947 Seconds

Expand  
Local $sBasedOn="Musashi", $iIter=1000000, $sOut, $t=TimerInit()

Local $input="10101010", $aArr, $dec

For $n=1 To $iIter
    $aArr = StringSplit($input, "", 2)
    $dec = 0
    For $i = UBound($aArr) - 1 To 0 Step -1
        If $aArr[$i] = "1" Then
           $dec = BitXOR($dec, BitShift(1, -(UBound($aArr) - 1 - $i)))
           $sOut=Hex($dec) &" "& $dec
        EndIf
    Next
Next

$t=TimerDiff($t)

ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)
  Quote

Based on code from: JockoDundee
 Check: AA 170 
 1000000 Iterations in 3.4197433 Seconds
Based on code from: JockoDundee
 Check: AA 170 
 1000000 Iterations in 3.3934325 Seconds
Based on code from: JockoDundee
 Check: AA 170 
 1000000 Iterations in 3.4096929 Seconds

Expand  
Local $sBasedOn="JockoDundee", $iIter=1000000, $sOut, $t=TimerInit()

Local $sbyte="10101010", $arr[1112]

$arr[0]="0"
$arr[1]="1"
$arr[10]="2"
$arr[11]="3"
$arr[100]="4"
$arr[101]="5"
$arr[110]="6"
$arr[111]="7"
$arr[1000]="8"
$arr[1001]="9"
$arr[1010]="A"
$arr[1011]="B"
$arr[1100]="C"
$arr[1101]="D"
$arr[1011]="E"
$arr[1111]="F"

For $n=1 To $iIter
   $sOut=$arr[StringLeft($sByte,4)] & $arr[StringRight($sByte,4)]
   $sOut=$sOut &" "& Dec($sOut)
Next

$t=TimerDiff($t)

ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)
  Quote

Based on code from: Chimp
 Check: AA 170 
 1000000 Iterations in 3.9945812 Seconds
Based on code from: Chimp
 Check: AA 170 
 1000000 Iterations in 4.080412 Seconds
Based on code from: Chimp
 Check: AA 170 
 1000000 Iterations in 4.0526451 Seconds

Expand  
Global $sBasedOn = "Chimp", $iIter = 1000000, $sOut, $t = TimerInit()
Global $sbyte = "10101010"

Global Enum $0000, $0001, $0010, $0011, $0100, $0101, $0110, $0111, $1000, $1001
Global $1010 = "A", $1011 = "B", $1100 = "C", $1101 = "D", $1110 = "E", $1111 = "F"

For $n = 1 To $iIter
    $sOut = Eval(StringLeft($sbyte, 4)) & Eval(StringRight($sbyte, 4))
    $sOut = $sOut & " " & Dec($sOut)
Next

$t = TimerDiff($t)

ConsoleWrite("Based on code from: " & $sBasedOn & @CRLF & " Check: " & $sOut & " " & @CRLF & " " & $iIter & " Iterations in " & $t / 1000 & " Seconds" & @CRLF & @CRLF)
  Quote

Based on code from: Chimp with major_lee edit
 Check: AA 170 
 1000000 Iterations in 11.2108708 Seconds
Based on code from: Chimp with major_lee edit
 Check: AA 170 
 1000000 Iterations in 11.2885781 Seconds
Based on code from: Chimp with major_lee edit
 Check: AA 170 
 1000000 Iterations in 11.0158957 Seconds

Expand  
Global $sBasedOn="Chimp with major_lee edit", $iIter = 1000000, $sOut, $t = TimerInit()

Global Enum $0000, $0001, $0010, $0011, $0100, $0101, $0110, $0111, $1000, $1001
Global Const $1010 = "A", $1011 = "B", $1100 = "C", $1101 = "D", $1110 = "E", $1111 = "F"
Global $dataStream = "10101010"


For $n = 1 To $iIter
    $sbyte = $dataStream
    $sOut = Null
    Local $bArray[StringLen ( $sbyte )/4+1]
    $bArray[0] = (StringLen ( $sbyte )/4)
    for $z = 1 To $bArray[0]
    $bArray[$z] = Eval(StringLeft($sbyte, 4))
    $sbyte = StringTrimLeft ( $sbyte, 4 )
    $sOut=$sOut&$bArray[$z]
    Next
    $sOut=$sOut &" "& Dec($sOut)
Next

$t = TimerDiff($t)
ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)

 

Edited by major_lee
benchmark for chimp typo, and jhcd $iIter
Posted (edited)

I wanna play too:

Tester()
Func Tester()
    Local $t, $sResult, $sbyteOriginal="10101010"
    Local $o = ObjCreate("Scripting.Dictionary")
    $o.Add("0000", "0")
    $o.Add("0001", "1")
    $o.Add("0010", "2")
    $o.Add("0011", "3")
    $o.Add("0100", "4")
    $o.Add("0101", "5")
    $o.Add("0110", "6")
    $o.Add("0111", "7")
    $o.Add("1000", "8")
    $o.Add("1001", "9")
    $o.Add("1010", "A")
    $o.Add("1011", "B")
    $o.Add("1100", "C")
    $o.Add("1101", "D")
    $o.Add("1110", "E")
    $o.Add("1111", "F")

    $t = TimerInit()
    For $n = 1 To 1000 ; >>> 9.7685  -  $sResult: AA
        $sResult = ""
        $sbyte = $sbyteOriginal
        Do
            $sResult &= $o.Item(StringLeft($sbyte, 4))
            $sbyte = StringTrimLeft($sbyte, 4)
        Until $sbyte = ""
    Next
    ConsoleWrite(">>> " &TimerDiff($t)& "  -  $sResult: " &  $sResult & @CRLF)
EndFunc

And, I'd win 1st price for slowest original idea :P 

Edit: since I don't wanna lose to myself, here is a yet slower ones:

  Reveal hidden contents

 

Edited by argumentum
am a sore loser/winner

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
  On 5/10/2021 at 10:50 PM, JockoDundee said:

 ... are you compiling 64-bit or 32-bit?  any other flags?

Expand  

All flags in my AutoIt/SciTE version (3.3.14.0) are on default (e.g. x64=No).
If I set the directive #AutoIt3Wrapper_UseX64 = Y , then your variant is faster than Chimp's (as you stated) :).

Based on code from: Chimp
-> with #AutoIt3Wrapper_UseX64 = Y (Default=N)
==> Trial 1 : 1000000 Iterations in 4.46783944157985 Seconds
==> Trial 2 : 1000000 Iterations in 4.4102539416485 Seconds
==> Trial 3 : 1000000 Iterations in 4.39570894432897 Seconds

 

Based on code from: JockoDundee
-> with #AutoIt3Wrapper_UseX64 = Y (Default=N)
==> Trial 1 : 1000000 Iterations in 3.6696164912397 Seconds
==> Trial 2 : 1000000 Iterations in 3.7065374362568 Seconds
==> Trial 3 : 1000000 Iterations in 3.6125747123171 Seconds

  On 5/11/2021 at 3:10 AM, argumentum said:

I wanna play too:

Expand  

Funny, I also tried it via a "Scripting.Dictionary Object" but it is, as you wrote, slow :lol:.

  Reveal hidden contents

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted
  On 5/10/2021 at 10:50 PM, JockoDundee said:

@Chimp, did you see any performance difference between the two on your machine?

 

Expand  

... your is a bit faster:

Based on code from: Chimp
 Check: AA 170
 100000 Iterations in 0.5807074 Seconds

Based on code from: JockoDundee
 Check: AA 170
 100000 Iterations in 0.5009863 Seconds

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted

This is cool, cause I'm learning a lot, all the different ways it can be done. I've never even considered using COM objects.

 

  Quote

Based on code from: argumentum
 Check: AA 170 
 1000000 Iterations in 10.8612472 Seconds
Based on code from: argumentum
 Check: AA 170 
 1000000 Iterations in 10.9447503 Seconds
Based on code from: argumentum
 Check: AA 170 
 1000000 Iterations in 10.5175166 Seconds

Expand  
Local $sBasedOn="argumentum", $iIter=1000000, $t=TimerInit()
Local $sResult, $sbyteOriginal="10101010", $dec
Local $o = ObjCreate("Scripting.Dictionary")
    $o.Add("0000", "0")
    $o.Add("0001", "1")
    $o.Add("0010", "2")
    $o.Add("0011", "3")
    $o.Add("0100", "4")
    $o.Add("0101", "5")
    $o.Add("0110", "6")
    $o.Add("0111", "7")
    $o.Add("1000", "8")
    $o.Add("1001", "9")
    $o.Add("1010", "A")
    $o.Add("1011", "B")
    $o.Add("1100", "C")
    $o.Add("1101", "D")
    $o.Add("1110", "E")
    $o.Add("1111", "F")

For $n=1 To $iIter
        $sResult = ""
        $sbyte = $sbyteOriginal
        Do
            $sResult &= $o.Item(StringLeft($sbyte, 4))
            $sbyte = StringTrimLeft($sbyte, 4)
        Until $sbyte = ""
$sOut = $sResult &" "& Dec($sResult)
Next

$t=TimerDiff($t)

ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)

 

Posted


I think its only fair to extend the code to handle more then the limited bytes as originally assumed.

 

  Quote

Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 10.7270697 Seconds
Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 10.7771624 Seconds
Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 10.7246844 Seconds

Expand  

 

Local $sBasedOn="JockoDundee with major_lee edit", $iIter=1000000, $sOut, $t=TimerInit()

Local $dataStream="10101010", $arr[1112]

$arr[0]="0"
$arr[1]="1"
$arr[10]="2"
$arr[11]="3"
$arr[100]="4"
$arr[101]="5"
$arr[110]="6"
$arr[111]="7"
$arr[1000]="8"
$arr[1001]="9"
$arr[1010]="A"
$arr[1011]="B"
$arr[1100]="C"
$arr[1101]="D"
$arr[1011]="E"
$arr[1111]="F"


For $n=1 To $iIter
    $sbyte = $dataStream
    $sOut = Null
    Local $bArray[StringLen ( $sbyte )/4+1]
    $bArray[0] = (StringLen ( $sbyte )/4)

    for $z = 1 To $bArray[0]
        $bArray[$z] = $arr[StringLeft($sbyte, 4)]
        $sbyte = StringTrimLeft ( $sbyte, 4 )
        $sOut=$sOut&$bArray[$z]
    Next
    $sOut=$sOut &" "& Dec($sOut)
Next

$t=TimerDiff($t)

ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...