not the fastest, but its speed is probably good enough for most of the tasks.
Code: Select all
bytes_per_char := A_IsUnicode ? 2 : 1
varsetcapacity(oritext, 5000000 * bytes_per_char)
loop, 100000
oritext .= "line " a_index " some text is better than none at all`n"
oritext .= mod(a_tickcount, 2) ? "(random last line test)" : ""
text := oritext
tooltip, stopwatch now
timebegin := a_tickcount
mReverseByLine(text)
lapse := a_tickcount - timebegin
tooltip
msgbox % "method one`n`ntime spent: " lapse " ms`ntotal length: " strlen(text) " chars`n" substr(text, 1, 1000) "..."
mReverseByLine(ByRef text, delimiter := "`n") {
text := text (SubStr(text, 0) = delimiter ? "" : delimiter), address := &text, srclen := StrLen(text), prolen := 0, bytes_per_char := A_IsUnicode ? 2 : 1
Loop, Parse, text, % delimiter
len := StrLen(A_LoopField), StrPut(A_LoopField delimiter, address + (srclen - prolen - len - 1) * bytes_per_char, len + 1), prolen += len + 1
}
text := oritext
tooltip, stopwatch now
timebegin := a_tickcount
sReverseByLine(text)
lapse := a_tickcount - timebegin
tooltip
msgbox % "method two`n`ntime spent: " lapse " ms`ntotal length: " strlen(text) " chars`n" substr(text, 1, 1000) "..."
sReverseByLine(ByRef text, delimiter := "`n") {
array := StrSplit(SubStr(text, 0) = delimiter ? SubStr(text, 1, StrLen(text) - 1) : text, delimiter), mi := array.MaxIndex()
Loop, % mi
If A_Index > 1
text .= array[mi - A_Index + 1] delimiter
Else text := array[mi] delimiter
}
text := oritext
tooltip, stopwatch now
timebegin := a_tickcount
Sort, text, F ReverseDirection
lapse := a_tickcount - timebegin
tooltip
msgbox % "method three`n`ntime spent: " lapse " ms`ntotal length: " strlen(text) " chars`n" substr(text, 1, 1000) "..."
ReverseDirection(a1, a2, offset)
{
return offset ; Offset is positive if a2 came after a1 in the original list; negative otherwise.
}
2021-7-7 edited: one more method is added.
after
@flyingDman's post, i've added his (the sort method from autohotkey documentation) as the third one in the benchmark.
for
@Chunjee, i don't know how to include yours. it will be nice if you can advise how. (better not the whole library but only the necessary extracts.) thanks.