Описание функции
FindData
Поиск бинарных данных в памяти, алгоритмы: Quick Search, Quick Search, Boyer-Moore, на основе Python FastSearch
TS(*HayStack, HayStackSize, *Needle, NeedleSize.l, Pos=0)
QS(*HayStack, HayStackSize, *Needle, NeedleSize.l, Pos=0)
BM(*HayStack, HayStackSize, *Needle, NeedleSize.u, Pos=0)
FastSearch(*HayStack, HayStackSize, *Needle, NeedleSize, Pos=0)
SSE2_Find(*HayStack, HayStackSize, *Needle, NeedleSize, Pos=0, Count=#False)
Параметры
*HayStack | Указатель на данные, в которых искать |
HayStackSize |
Размер данных, в которых искать |
*Needle |
Указатель на данные, которые искать |
NeedleSize |
Размер данных, которые искать |
Pos |
Позиция, от которой начать поиск |
Возвращаемое значение
Успех: | Возвращает позицию найденного |
Ошибка: | Возвращает -1 - не найдено |
Пример
; Wilbert
; https://www.purebasic.fr/english/viewtopic.php?p=525097#p525097
DisableDebugger
XIncludeFile "FindData.pbi"
HayStackSize = 65536
NeedleSize = 64
*HayStack = AllocateMemory(HayStackSize)
*Needle = AllocateMemory(NeedleSize)
RandomSeed(0)
RandomData(*HayStack, HayStackSize)
CopyMemory(*HayStack + HayStackSize - NeedleSize - 32, *Needle, NeedleSize)
t0 = ElapsedMilliseconds()
For i = 1 To 20000
x = FindData::TS(*HayStack, HayStackSize, *Needle, NeedleSize)
Next
t1 = ElapsedMilliseconds()
For i = 1 To 20000
x = FindData::QS(*HayStack, HayStackSize, *Needle, NeedleSize)
Next
t2 = ElapsedMilliseconds()
For i = 1 To 20000
x = FindData::BM(*HayStack, HayStackSize, *Needle, NeedleSize)
Next
t3 = ElapsedMilliseconds()
For i = 1 To 20000
x = FindData::FastSearch(*HayStack, HayStackSize, *Needle, NeedleSize)
Next
t4 = ElapsedMilliseconds()
For i = 1 To 20000
x = FindData::SSE2_Find(*HayStack, HayStackSize, *Needle, NeedleSize)
Next
t5 = ElapsedMilliseconds()
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
S.s = "Windows "
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
S.s = "OSX "
CompilerElse
S.s = "Linux "
CompilerEndIf
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
S + "(x86) "
CompilerElse
S + "(x64) "
CompilerEndIf
S + #CRLF$ + CPUName() + #CRLF$ + #CRLF$
S + "Test 1" + #CRLF$
S + "----------" + #CRLF$
S + "Tailed Substring : " + Str(t1-t0) + "ms" + #CRLF$
S + "QuickSearch : " + Str(t2-t1) + "ms" + #CRLF$
S + "Boyer-Moore : " + Str(t3-t2) + "ms" + #CRLF$
S + "FastSearch : " + Str(t4-t3) + "ms" + #CRLF$
S + "SSE2 Find : " + Str(t5-t4) + "ms" + #CRLF$
PokeS(*HayStack, "A small test with a small haystack and short needle. This will perform differently because some algorithms take some time to initialize", -1, #PB_Ascii)
PokeS(*Needle, "initial", -1, #PB_Ascii)
HayStackSize2 = MemoryStringLength(*HayStack, #PB_Ascii)
NeedleSize2 = MemoryStringLength(*Needle, #PB_Ascii)
t0 = ElapsedMilliseconds()
For i = 1 To 1500000
x = FindData::TS(*HayStack, HayStackSize2, *Needle, NeedleSize2)
Next
t1 = ElapsedMilliseconds()
For i = 1 To 1500000
x = FindData::QS(*HayStack, HayStackSize2, *Needle, NeedleSize2)
Next
t2 = ElapsedMilliseconds()
For i = 1 To 1500000
x = FindData::BM(*HayStack, HayStackSize2, *Needle, NeedleSize2)
Next
t3 = ElapsedMilliseconds()
For i = 1 To 1500000
x = FindData::FastSearch(*HayStack, HayStackSize2, *Needle, NeedleSize2)
Next
t4 = ElapsedMilliseconds()
For i = 1 To 1500000
x = FindData::SSE2_Find(*HayStack, HayStackSize2, *Needle, NeedleSize2)
Next
t5 = ElapsedMilliseconds()
S + #CRLF$
S + "Test 2" + #CRLF$
S + "----------" + #CRLF$
S + "Tailed Substring : " + Str(t1-t0) + "ms" + #CRLF$
S + "QuickSearch : " + Str(t2-t1) + "ms" + #CRLF$
S + "Boyer-Moore : " + Str(t3-t2) + "ms" + #CRLF$
S + "FastSearch : " + Str(t4-t3) + "ms" + #CRLF$
S + "SSE2 Find : " + Str(t5-t4) + "ms" + #CRLF$
SetClipboardText(S)
MessageRequester("Test results", S)