03-07-2025, 07:53 PM
I don't know for what it will, but it's such trivial stupidity... If you want it faster, you have to choose an even better method - for example, filtering. But I won't deal with that. Here is a basic version, of course, if you are going to search gigabyte and larger arrays, you must waiting to output with this version.
Because I don't have enough knowledge to apply the Boyer-Moore or Boyer-Moore-Horspool algorithm, or deal with parallelization to increase speed right now. Practically speaking, if you are going to search Gigabyte and larger arrays, divide them into smaller ones but don't forget about the overlap (if it happens that half of the sample you are looking for will lie at the end of the smaller block and the other half at the beginning of the next block during the next reading) - without the overlap you would never find the sample. But in general - if you really need lightning-fast output in your program, this is a stupid solution. Just save the offset when storing to memory or choose small memory blocks - they will be searched faster.
Because I don't have enough knowledge to apply the Boyer-Moore or Boyer-Moore-Horspool algorithm, or deal with parallelization to increase speed right now. Practically speaking, if you are going to search Gigabyte and larger arrays, divide them into smaller ones but don't forget about the overlap (if it happens that half of the sample you are looking for will lie at the end of the smaller block and the other half at the beginning of the next block during the next reading) - without the overlap you would never find the sample. But in general - if you really need lightning-fast output in your program, this is a stupid solution. Just save the offset when storing to memory or choose small memory blocks - they will be searched faster.
Code: (Select All)
Dim m As _MEM
Dim As _Offset p
Dim As _Unsigned _Integer64 c, Offset, BinPos
m = _MemNew(100000000) '100 Mbyte block
Do Until p = m.SIZE - 8 'fill memory with some random data
c = 18446744073709551615 * Rnd
_MemPut m, m.OFFSET + p, c
p = p + 8
Loop
Binaries$ = "World!"
BinPos = (m.SIZE - Len(Binaries$)) * Rnd 'get random offset
Print "Random Offset: "; BinPos
_MemPut m, m.OFFSET + BinPos, Binaries$ 'place data to ram
Print "Searching string: "; Binaries$
Offset = SearchOffset(m, Binaries$)
Dim Test As String
Test = Space$(Len(Binaries$))
Print "Function return Offset: "; Offset
_MemGet m, m.OFFSET + Offset, Test$
Print "Output: (function) "; Test$
Print "Random offset in begin: "; BinPos
_MemGet m, m.OFFSET + BinPos, Test$
Print "Output: (random in begin) "; Test$
Function SearchOffset~&& (N As _MEM, b As String)
Dim srch As String
Dim Result As _Offset
Dim J As Long
srch = Space$(Len(b))
fca = Asc(b, 1)
Do Until J >= N.SIZE - Len(b)
Do Until _MemGet(N, N.OFFSET + J, _Unsigned _Byte) = fca
J = J + 1
Loop
_MemGet N, N.OFFSET + J, srch$
Result = InStr(1, srch$, b)
If Result Then SearchOffset = J + Result - 1: Exit Function
J = J + 1
Loop
End Function