Re: Compare String
Posted: Wed May 01, 2013 9:40 pm
you need to test the map approach so that it's sorting the data while it's being read in from file, using it after won't be faster.
However to make it faster will probably require you to use a thread and buffer the file stream so it can process the stream asynchronously.
something like this but optimized
However to make it faster will probably require you to use a thread and buffer the file stream so it can process the stream asynchronously.
something like this but optimized
Code: Select all
Global NewMap Ma.s()
Global NewList listCombined()
Global NewList Buffer.s()
Global mbuffer = CreateMutex()
Global sbuffer = CreateSemaphore()
Global quit
Procedure Filllist(s1.s)
If Not FindMapElement(Ma(),s1)
AddMapElement(Ma(),s1)
AddElement(listCombined())
Ma() = s1
listCombined() = @ma()
EndIf
EndProcedure
Procedure Push(input.s)
LockMutex(mbuffer)
LastElement(Buffer())
AddElement(Buffer())
buffer() = input
UnlockMutex(mbuffer)
SignalSemaphore(sbuffer)
EndProcedure
Procedure.s Pop()
Protected out.s
LockMutex(mBuffer)
If ListSize(buffer())
FirstElement(Buffer())
out = Buffer()
DeleteElement(buffer())
EndIf
UnlockMutex(mbuffer)
ProcedureReturn out
EndProcedure
Procedure Sortdata(void.i)
Protected input.s
Repeat
WaitSemaphore(sbuffer)
input = pop()
If input <> ""
Filllist(input)
EndIf
Until quit
EndProcedure
Procedure ReadFromFile()
CreateThread(@Sortdata(),0)
For a = 1 To 100
push("some string " + Str(a))
Delay(1) ;simulate a delay note file reads are done into a buffer of at least 4096k
Next
For a = 50 To 150
push("some string " + Str(a))
Delay(1)
Next
quit = 1
SignalSemaphore(sbuffer)
EndProcedure
Define *ps.string
Debug "the combined lists output"
ReadFromFile()
ForEach listCombined()
*ps = listCombined()
Debug *ps\s
Next