[Speedtest] Bitwise Hacks

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

[Speedtest] Bitwise Hacks

18 Sep 2017, 08:00

Good point Helgef =)

Code: Select all

p := 1
loop 11
{
    str .= (A_Index - 1) "`t" p "`n"
    p <<= 1
}
MsgBox % str
But not every BitwiseHack is faster -> https://autohotkey.com/boards/viewtopic ... 641#p24641

More useful links:
Bitwise gems – fast integer math
Bitwise Operations in C

@jeeswg..
As long you dont use SetBatchLines -1 (in v1.1) your speedtest is not really correct...
Also msdn recommend using QPC and not TickCount (Acquiring high-resolution time stamps) for timestamps
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Easy Question about involution

18 Sep 2017, 13:43

@jNizM: Great links, re. bitwise.

Tests that use A_TickCount and don't use SetBatchLines are usually good enough to clearly establish which technique is faster. But I welcome your comments, and I think we should have a 'perfect benchmark test conditions' thread.

Do you have a list of perfect conditions? Or info re. SetBatchLines?

Last time I checked, I couldn't find a method that works in Windows XP/7, that gives results in milliseconds/microseconds/nanoseconds. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Easy Question about involution

18 Sep 2017, 13:53

Or info re. SetBatchLines?
Jeeswg, an example just for you. :angel:
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Easy Question about involution

19 Sep 2017, 01:31

OT

Example 1

Code: Select all

; ===============================================================================================================================

#NoEnv
SetBatchLines -1

global x := y := Frequency := CounterStart1 := CounterStop1 := CounterStart2 := CounterStop2 := 0
global init := DllCall("QueryPerformanceFrequency", "int64*", Frequency)

; ===============================================================================================================================

DllCall("QueryPerformanceCounter", "int64*", CounterStart1)

loop 10000000
    x := 10 * 64    ; <- 640

DllCall("QueryPerformanceCounter", "int64*", CounterStop1)

; ========================================================================

DllCall("QueryPerformanceCounter", "int64*", CounterStart2)

loop 10000000
    y := 10 << 6    ; <- 640

DllCall("QueryPerformanceCounter", "int64*", CounterStop2)

; ===============================================================================================================================

MsgBox % "Test 1:`t" ((CounterStop1 - CounterStart1) / Frequency) "`n"
       . "Test 2:`t" ((CounterStop2 - CounterStart2) / Frequency)

ExitApp

; ===============================================================================================================================
Example 2

Code: Select all

; ===============================================================================================================================

#NoEnv
SetBatchLines -1

global x := y := Frequency := CounterStart1 := CounterStop1 := CounterStart2 := CounterStop2 := 0
global init := DllCall("QueryPerformanceFrequency", "int64*", Frequency)

; ===============================================================================================================================

DllCall("QueryPerformanceCounter", "int64*", CounterStart1)

loop 1000000
    x := Mod(131, 4)      ; <- 3

DllCall("QueryPerformanceCounter", "int64*", CounterStop1)

; ========================================================================

DllCall("QueryPerformanceCounter", "int64*", CounterStart2)

loop 1000000
    y := 131 & (4 - 1)    ; <- 3

DllCall("QueryPerformanceCounter", "int64*", CounterStop2)

; ===============================================================================================================================

MsgBox % "Test 1:`t" ((CounterStop1 - CounterStart1) / Frequency) "`n"
       . "Test 2:`t" ((CounterStop2 - CounterStart2) / Frequency)

ExitApp

; ===============================================================================================================================
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Easy Question about involution

19 Sep 2017, 05:20

Hello jNizM :wave: . Nice tests, I think it would be more interesting to test 1 << 6 vs 2**6 instead of 10 << 6 vs 10*64 though. And if you replace (4-1) with 3 in the second example, there seem to be a very small gain to use the bit operator vs the mod function.
Cheers.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Easy Question about involution

19 Sep 2017, 06:58

Code: Select all

loop 10000000
    x := 2 ** 6

Average -> 1.155872
Min     -> 1.126823
Max     -> 1.211747

Code: Select all

loop 10000000
    y := 1 << 6

Average -> 0.729727
Min     -> 0.714059
Max     -> 0.790297

Code: Select all

loop 10000000
    x := Mod(131, 4)

Average -> 0.838374
Min     -> 0.800413
Max     -> 0.964131

Code: Select all

loop 10000000
    y := 131 & 3

Average -> 0.711950
Min     -> 0.700033
Max     -> 0.745709
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333 and 274 guests