Code: Select all
StandardDeviation(arr) {
mean := sd2 := 0
Loop(arr.Length) {
mean += arr[A_Index]
}
mean /= arr.Length
Loop(arr.Length) {
sd2 += (arr[A_Index] - mean) ** 2
}
return sd := sqrt(sd2 /= arr.Length)
}
Code: Select all
StandardDeviation(arr) {
mean := sd2 := 0
Loop(arr.Length) {
mean += arr[A_Index]
}
mean /= arr.Length
Loop(arr.Length) {
sd2 += (arr[A_Index] - mean) ** 2
}
return sd := sqrt(sd2 /= arr.Length)
}
Code: Select all
StandardDeviation(arr) {
mean := sd2 := 0
for val in arr
mean += val
mean /= arr.Length
for val in arr
sd2 += (val - mean) ** 2
return sqrt(sd2 / arr.Length)
}
Code: Select all
DefineStatistics()
DefineStatistics() {
Array.Prototype.DefineProp('mean', {get: mean})
Array.Prototype.DefineProp('sd', {get: sd})
mean(arr) {
local mean := 0
for val in arr
mean += val
return mean / arr.length
}
sd(arr) {
local sd := 0, mean := arr.mean
for val in arr
sd += (val - mean) ** 2
return sqrt(sd / arr.length)
}
}
arr := [1,2,3,4]
MsgBox arr.mean '`n' arr.sd
Code: Select all
arr := PopulateArray(100000)
DllCall("QueryPerformanceFrequency", "Int64*", &freq := 0)
DllCall("QueryPerformanceCounter", "Int64*", &CounterBefore := 0)
LoopStandardDeviation(arr)
DllCall("QueryPerformanceCounter", "Int64*", &CounterAfter := 0)
time1 := (CounterAfter - CounterBefore) / freq * 1000
DllCall("QueryPerformanceFrequency", "Int64*", &freq := 0)
DllCall("QueryPerformanceCounter", "Int64*", &CounterBefore := 0)
ForStandardDeviation(arr)
DllCall("QueryPerformanceCounter", "Int64*", &CounterAfter := 0)
time2 := (CounterAfter - CounterBefore) / freq * 1000
MsgBox("Standard Deviation Calculator`n`n Loop: " time1 "`n For-Loop: " time2)
PopulateArray(amount) {
arr := []
Loop(amount) {
arr.push(Random(0,100))
}
return arr
}
LoopStandardDeviation(arr) {
mean := sd2 := 0
Loop(arr.Length) {
mean += arr[A_Index]
}
mean /= arr.Length
Loop(arr.Length) {
sd2 += (arr[A_Index] - mean) ** 2
}
return sd := sqrt(sd2 /= arr.Length)
}
ForStandardDeviation(arr) {
mean := sd2 := 0
for val in arr
mean += val
mean /= arr.Length
for val in arr
sd2 += (val - mean) ** 2
return sqrt(sd2 / arr.Length)
}
Return to “Scripts and Functions (v2)”
Users browsing this forum: gdqb521 and 14 guests