Old code
Class version:
Code: Select all
#singleInstance ignore
#include tinyBenchClass.ahk
setBatchLines -1
setWorkingDir,% a_scriptDir
tb:=new tinyBench("strI",100) ; file name, iteration count
; code setup - if any static variables are needed, set them here
; userInfo will be logged in the output
iv:=randStr(100000,100000,1234)
tb.userInfo:="StrLen: " . strLen(iv)
loop % tb.iterations {
tb.start()
; code to be benchmarked
strI(iv)
; end code
tb.end()
}
tb.output()
msgbox,4,Finished,% "Benchmark for " . tb.name . " completed! Open log now?"
ifMsgBox Yes
run % tb.file
exitApp
; https://github.com/Masonjar13/AHK-Library/blob/master/Lib/strI.ahk
strI(str){
static rev:=a_isUnicode?"_wcsrev":"_strrev"
dllCall("msvcrt.dll\" . rev,"Ptr",&str,"CDECL")
return str
}
; https://github.com/Masonjar13/AHK-Library/blob/master/Lib/randStr.ahk
/* Modes
- Append numbers to combine
1=lowercase
2=uppercase
3=digits
4=symbols
example: randStr(1,5,124)
test:
rr:=[1,2,3,4,12,13,14,23,24,34,123,124,134,1234]
for i,a in rr
msgbox % a "`n" randStr(10,20,a)
*/
randStr(lowerBound,upperBound,mode=1){
if(!isDigit(lowerBound)||!isDigit(upperBound)||!isDigit(mode))
return -1
loop % rand(lowerBound,upperBound){
t:=""
if(strLen(mode)=1)
t:=mode
else{
while(!ifContains(mode,t))
t:=rand(1,4)
}
if(t=1)
str.=chr(rand(97,122))
else if(t=2)
str.=chr(rand(65,90))
else if(t=3)
str.=rand(0,9)
else if(t=4){
i:=rand(1,4)
str.=i=1?chr(rand(33,47)):i=2?chr(rand(58,64)):i=3?chr(rand(91,96)):chr(rand(123,126))
}
}
return str
}
; https://github.com/Masonjar13/AHK-Library/blob/master/Lib/isDigit.ahk
isDigit(in){
if in is digit
return 1
}
; https://github.com/Masonjar13/AHK-Library/blob/master/Lib/rand.ahk
rand(lowerBound,upperBound){
random,rand,% lowerBound,% upperBound
return rand
}
; https://github.com/Masonjar13/AHK-Library/blob/master/Lib/ifContains.ahk
ifContains(haystack,needle){
if haystack contains %needle%
return 1
}
Code: Select all
class tinyBench{
__new(name,iterations:=""){
dllCall("QueryPerformanceFrequency","Int64*",f),this.freq:=f
this.userInfo:="<UserInfo>"
this.name:=name
if(iterations)
this.iterations(iterations)
this.timings:=object()
}
iterations(iterationCnt){
this.timings.setCapacity(this.iterations:=iterationCnt)
}
start(){
this.ts:=a_tickCount,dllCall("QueryPerformanceCounter","Int64*",s),this.s:=s
}
end(){
dllCall("QueryPerformanceCounter","Int64*",e),te:=a_tickcount
this.timings.push({µs: ((e-this.s)*1000000)/this.freq
,ms: ((e-this.s)*1000)/this.freq
,mst: te-this.ts})
}
output(){
for i,a in this.timings{
µsA+=a.µs
µsL:=!µsL?a.µs:a.µs<µsL?a.µs:µsL
µsH:=a.µs>usH?a.µs:µsH
msA+=a.ms
msL:=!msL?a.ms:a.ms<msL?a.ms:msL
msH:=a.ms>msH?a.ms:msH
mstA+=a.mst
mstL:=!mstL?a.mst:a.mst<mstL?a.mst:mstL
mstH:=a.mst>mstH?a.mst:mstH
}
µsA/=this.timings.length()
msA/=this.timings.length()
mstA/=this.timings.length()
fileAppend,% this.userInfo
. "`nIterations: " . this.timings.length()
. "`nQPC (µs)`n • Average Time Elapsed: " . commaFormat(µsA)
. "`n • Lowest Time Elapsed: " . commaFormat(µsL)
. "`n • Highest Time Elapsed: " . commaFormat(µsH)
. "`nQPC (ms)`n • Average Time Elapsed: " . commaFormat(msA)
. "`n • Lowest Time Elapsed: " . commaFormat(msL)
. "`n • Highest Time Elapsed: " . commaFormat(msH)
. "`nTickCount (ms)`n • Average Time Elapsed: " . commaFormat(mstA)
. "`n • Lowest Time Elapsed: " . commaFormat(mstL)
. "`n • Highest Time Elapsed: " . commaFormat(mstH)
. "`n`n",% this.file:=this.name . "_tinyBench.txt"
}
}
; https://github.com/Masonjar13/AHK-Library/blob/master/Lib/commaFormat.ahk
commaFormat(num){
num:=strSplit(num,.)
nnum:=num[1]
numlen:=strlen(nnum)
loop,parse,nnum
xnum.=!mod(numlen-a_index,3) && a_index != numlen ? a_loopfield "," : a_loopfield
return num[2]?xnum "." num[2]:xnum
}