Jon said:
Why do you think that multi-threading would help you? It doesn't sound
like the kind of task which is actually helped much by multi-threading.
Chances are you'll be limited by disk access more than anything else,
and multi-threading that is likely to make it worse rather than better.
See
http://www.pobox.com/~skeet/csharp/threads/whentouse.shtml
==================== mergesort ===============================
Imports System
Imports System.Collections
Imports System.IO
Imports System.Threading
Module MergeSortApp
Sub Main()
Dim czas As DateTime
Dim roznica As String
Console.Clear()
Console.WriteLine("MergeSort VB.NET - wersja wielowÄ…tkowa")
Console.WriteLine("")
Console.WriteLine("Paweł Janowski - 9875")
Console.WriteLine()
' pobieranie nazw plików
Dim szSrcFile, szDestFile As String
szSrcFile = "miasta_unsorted.txt"
szDestFile = "miasta_sorted.txt"
' odczyt pliku źródłowego
Dim szSrcLine As String
Dim szContents As ArrayList
Dim fsInput As FileStream
Dim srInput As StreamReader
szContents = New ArrayList()
Try
fsInput = New FileStream(szSrcFile, FileMode.Open,
FileAccess.Read)
Catch ex As Exception
Console.WriteLine("nie moge odczytać pliku źródłowego")
Exit Sub
End Try
srInput = New StreamReader(fsInput)
szSrcLine = srInput.ReadLine()
While Not IsNothing(szSrcLine)
' tworzenie tablicy
szContents.Add(szSrcLine)
szSrcLine = srInput.ReadLine()
End While
srInput.Close()
fsInput.Close()
'sortowanie
dim oMergeSortClass as new MergeSortClass
dim t as new Thread(AddressOf oMergeSortClass.MergeSort)
oMergeSortClass.szArray = szContents
oMergeSortClass.nLower = 0
oMergeSortClass.nUpper = szContents.Count - 1
Console.ReadKey()
czas = Now()
t.Start()
t.Join()
roznica = dateDiff("s", czas, Now()).ToString
' zapisywanie posortowanych linijek
Dim fsOutput As FileStream
Dim srOutput As StreamWriter
Dim nIndex As Integer
Try
fsOutput = New FileStream(szDestFile, FileMode.Create,
FileAccess.Write)
Catch ex As Exception
Console.WriteLine("nie moge zapisać do pliku docelowego")
Exit Sub
End Try
srOutput = New StreamWriter(fsOutput)
For nIndex = 0 To szContents.Count - 1
' zapis po linijce
srOutput.WriteLine(szContents(nIndex))
Next nIndex
srOutput.Close()
fsOutput.Close()
Console.WriteLine()
Console.WriteLine("Posortowane linie zostały zapisane do pliku
wynikowego")
Console.WriteLine()
Console.WriteLine("Całość trwała " & roznica & " sekund")
Console.ReadKey()
end
End Sub
Class MergeSortClass
Public Event ThreadComplete(ByVal szArray As ArrayList)
Public szArray As ArrayList
Public nLower As Integer
Public nUpper as Integer
Sub MergeSort()
System.Threading.Thread.Sleep(25)
Console.Write("+")
Dim szSwap As String
If (nUpper - nLower) = 1 Then
If szArray(nLower).CompareTo(szArray(nUpper)) > 0 Then
szSwap = szArray(nLower)
szArray(nLower) = szArray(nUpper)
szArray(nUpper) = szSwap
End If
ElseIf (nUpper - nLower) > 1 Then
dim oMergeSortClass1 as new MergeSortClass
dim t1 as new Thread(AddressOf oMergeSortClass1.MergeSort)
oMergeSortClass1.szArray = szArray
oMergeSortClass1.nLower = nLower
oMergeSortClass1.nUpper = (nLower + nUpper) / 2
t1.Start()
t1.Join()
dim oMergeSortClass2 as new MergeSortClass
dim t2 as new Thread(AddressOf oMergeSortClass2.MergeSort)
oMergeSortClass2.szArray = szArray
oMergeSortClass2.nLower = (nLower + nUpper) / 2 + 1
oMergeSortClass2.nUpper = nUpper
t2.Start()
t2.Join()
Dim oMergeClass As New MergeClass
oMergeClass.szArray = szArray
oMergeClass.nLower = nLower
oMergeClass.nMiddle = (nLower + nUpper) / 2 + 1
oMergeClass.nUpper = nUpper
Dim t As New Thread(AddressOf oMergeClass.Merge)
t.Start()
t.Join()
End If
RaiseEvent ThreadComplete(szArray)
End Sub
end Class
Class MergeClass
Public Event ThreadComplete(ByVal szArray As ArrayList)
Public szArray As ArrayList
Public nLower As Integer
Public nMiddle As Integer
Public nUpper
Sub Merge()
System.Threading.Thread.Sleep(25)
Console.Write("-")
Dim nIndex As Integer
Dim szBuffer As New ArrayList()
Dim i, j As Integer
i = nLower
j = nMiddle
While i < nMiddle And j <= nUpper
If (szArray(i).CompareTo(szArray(j)) < 0) Then
szBuffer.Add(szArray(i))
i = i + 1
Else
szBuffer.Add(szArray(j))
j = j + 1
End If
End While
While i < nMiddle
szBuffer.Add(szArray(i))
i = i + 1
End While
While j <= nUpper
szBuffer.Add(szArray(j))
j = j + 1
End While
nIndex = 0
For i = nLower To nUpper
szArray(i) = szBuffer(nIndex)
nIndex = nIndex + 1
Next i
RaiseEvent ThreadComplete(szArray)
End Sub
End Class
End Module
==================== mergesort ===============================
miasta_unsorted.txt (input file)
miasta_sorted.txt (output file)
please send me Ur's opinion.
w/regards
Paul