Hello everyone.
Not really asking for code, but I'm interested in knowing the basics of "thread performance".
For example, you try to copy a bitmap in a pixel by pixel operation. (Is slow, but it is a nice example)
This litterally takes ages on a 1280 x 1024 bitmap, but what if I would use 1024 separate threads performing the copy of a single line?:Code:Dim IMG As Bitmap = Image.FromFile("c:\test.png") Dim COPY As New Bitmap(IMG.Width, IMG.Height) For X As Integer = 0 To IMG.Width - 1 For Y As Integer = 0 To IMG.Height - 1 COPY.SetPixel(X, Y, IMG.GetPixel(X, Y)) Next Next
This is, of course, very buggy and causes a lot of violation errors etc. But that doesn't matter right now. How come the multi-threading way is so much faster than the regular routine? Is the processor not running on full speed with the first code, and is it working harder on the second?Code:Private IMG As Bitmap Private COPY As Bitmap Private started As Boolean Private line As Integer Private finishcount As Integer Private Sub CopyLine() Dim Y As Integer = line started = True For X As Integer = 0 To IMG.Width - 1 Dim succesfull As Boolean = False Do While succesfull = False Try COPY.SetPixel(X, Y, IMG.GetPixel(X, Y)) succesfull = True Catch End Try Loop Next finishcount += 1 End Sub Private Sub CopyImage() IMG = Image.FromFile("c:\test.png") COPY = New Bitmap(IMG.Width, IMG.Height) finishcount = 0 For Y As Integer = 0 To IMG.Height - 1 line = Y started = False Dim t As New Threading.Thread(AddressOf CopyLine) t.IsBackground = True t.Start() Do While started = False Threading.Thread.Sleep(50) Loop Next 'all threads launched, now waiting for it to finish Do While finishcount < IMG.Height Threading.Thread.Sleep(500) Loop MessageBox.Show("Copying finished.", "Finished.", MessageBoxButtons.OK) End Sub
I find it weird to see this type of "threading performance increase", as if every single thread gets their own piece of processor speed allocated like it is a new process. Is there anyone who knows more of this?




Reply With Quote