/*
Name : MBM - Minimal Benchmark
Version : v0.2
Author : R3gX
Thanks to : just me & SKAN for QPC and QPX codes
*/
; INITIALIZE GUI
MBM_GUI_Init()
MBM(subs="", iterations="")
{
global MBM_GUI_ID
static BMCount := 0 , PrevBM := ""
; CHECK PARAMETERS
If (not IsObject(subs) && not IsLabel(subs))
Return
Else If (not IsObject(subs) && IsLabel(subs))
subs := [subs]
If not (iterations~="^\d+$")
iterations := MBM_Vars("iterations")
; SET LOOP'S VARS
BatchLines := A_BatchLines
If MBM_Vars("MaxSpeed")
SetBatchLines, -1
BMCount++ , BMHeader := "BENCHMARK #" BMCount , BM := ""
WinSetTitle, % "ahk_id " MBM_GUI_ID,, % BMHeader
Loop, % iterations
{
iteration := A_Index , log := ""
For i,sub in subs
{
If !IsLabel(sub)
Continue
; BENCHMARK
MBM_Times(sub)
GoSub, % sub
times := MBM_Times(sub, iteration)
; FORMAT THE BENCHMARK TEXT
step := "[" iteration "." A_Index "]"
log .= sub "`t" times.average "`n"
BM := BMCount>1 ? PrevBM BMHeader "`n" : ""
BM .= step "`n" log "`n"
MBM_GUI( BM )
DllCall("Sleep", "UInt", 10)
}
}
; SAVE PREVIOUS BENCHMARK TEXT TO PREPEND IT LATER
PrevBM .= BMHeader "`n" step "`n" log "`n"
; RESTORE SCRIPT'S DEFAULT SPEED
SetBatchLines, % BatchLines
WinSetTitle, % "ahk_id " MBM_GUI_ID,, % BMHeader " - ENDED!"
}
MBM_Vars(key="", value="")
{
static vars := { "iterations" : 10
, "MaxSpeed" : 1
, "FormatTime" : 1
, "DisplayHour" : 0
, "SaveOnExit" : 1
, "OutputFile" : A_ScriptDir "\MBM_Logs.txt" }
If !vars.HasKey(key)
Return
If ( (key="iterations" and value~="^\d+$")
or (key="MaxSpeed" and value~="^[01]$")
or (key="FormatTime" and value~="^[01]$")
or (key="DisplayHour" and value~="^[01]$")
or (key="SaveOnExit" and value~="^[01]$") )
Return, (vars[key] := value)
Else
Return, vars[key]
}
MBM_Times(sub="", iter="")
{
static IsStarted := {} , tot := {}
If not IsStarted[sub]
IsStarted[sub] := MBM_QPT()
Else If (IsStarted[sub] && iter~="^\d+$")
{
IsStarted[sub] := 0
elapsed := MBM_QPT()
tot[sub] ? (tot[sub]+=elapsed) : (tot[sub]:=elapsed)
average := tot[sub]/iter
If MBM_Vars("FormatTime")
elapsed := MBM_FormatTime(elapsed)
, average := MBM_FormatTime(average)
Return, { "elapsed" : elapsed , "average" : average }
}
}
/*
sligthly modified version from 'just me'
>> http://www.autohotkey.com/board/topic/
82483-fileread-vs-loop-read/page-2#entry524667
---------------------------------------------------------------
See also QPX() from 'SKAN'
>> http://www.autohotkey.com/board/topic/
91860-object-qpx-test-how-fast-the-code-runs/#entry579284
---------------------------------------------------------------
See also QPX() from 'Learning one'
>> http://www.autohotkey.com/board/topic/
91860-object-qpx-test-how-fast-the-code-runs/?hl=qpx
*/
MBM_QPT()
{
Static Frequency := 0 , Previous := 0
If !(Frequency)
DllCall("Kernel32.dll\QueryPerformanceFrequency", "Int64P", Frequency)
DllCall("Kernel32.dll\QueryPerformanceCounter", "Int64P", Counter)
Interval := (Counter-Previous)/Frequency , Previous := Counter
Return Interval
}
MBM_FormatTime(_secs)
{
hour := _secs//3600
hour_mod := Mod(_secs, 3600)
min := hour_mod//60
min_mod := Mod(hour_mod, 60)
sec := min_mod//1
sec_mod := Mod(min_mod, 1)
msec := (sec_mod*1000)//1
msec_mod := ((sec_mod*1000)-msec)*1000
FloatFormat := A_FormatFloat
SetFormat, Float, % 02
hour += 0 , min += 0 , sec += 0
SetFormat, Float, % 03
msec += 0 , msec_mod += 0
SetFormat, Float, % FloatFormat
Time := MBM_Vars("DisplayHour")==1 ? hour ":" : ""
Return, Time .= min ":" sec ":" msec "." msec_mod
}
MBM_GUI_Init()
{
global MBM_GUI_ID, MBM_GUI_edit
GuiStyles := "+LabelMBM_GUI_ +LastFound +HwndMBM_GUI_ID"
GuiStyles .= " +Resize +AlwaysOnTop +ToolWindow"
Gui, MBM_GUI:Destroy
Gui, MBM_GUI:New, % GuiStyles
Gui, MBM_GUI:Margin, 0, 0
Gui, MBM_GUI:Font,, DejaVu Sans Mono
Gui, MBM_GUI:Add, Edit, w400 h200 vMBM_GUI_edit +ReadOnly +Multi
Gui, MBM_GUI:Show, x0 yCenter, MBM
Gui, 1:Default
Menu, Tray, Add, Show GUI, MBM_GUI_Show
}
MBM_GUI(Text="")
{
GuiControl, MBM_GUI:, MBM_GUI_edit, % Text
}
MBM_GUI_Show()
{
MBM_GUI_Show:
Gui, MBM_GUI:Show,, MBM
Return
}
MBM_GUI_Escape()
{
MBM_GUI_Escape:
Gui, MBM_GUI:Submit
Return
}
MBM_GUI_Size()
{
MBM_GUI_Size:
GuiControl, MBM_GUI:Move, MBM_GUI_edit, % "w" A_GuiWidth " h" A_GuiHeight
Return
}
MBM_GUI_Close()
{
MBM_GUI_Close:
MsgBox, 262436, EXIT:, Exit script?, 1
IfMsgBox No
Return
If MBM_Vars("SaveOnExit")
{
GuiControlGet, BM, MBM_GUI:, MBM_GUI_edit
Header := "===== BENCHMARK " A_Now " ====="
Footer := "===================================="
BM := "`n" Header "`n" Trim(BM, " `t`n") "`n" Footer "`n"
FileAppend, % "`n" BM, % MBM_Vars("OutputFile")
}
ExitApp
Return
}