Results 1 to 5 of 5

Thread: Threading with For Loop vs. For Loop only. ( VB.NET 2010 )

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    4

    Threading with For Loop vs. For Loop only. ( VB.NET 2010 )

    Hi, i'm doing some experimentation with threading and for loop,
    i'm having curiosity why there's different a different on speed between "Threading With For Loop" against "For Loop Only"

    any suggestion/comments that can be help or modifying in the code will be a thankful in my part.. thank you.

    I uploaded the code..
    Attached Files Attached Files

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,763

    Re: Threading with For Loop vs. For Loop only. ( VB.NET 2010 )

    I haven't looked at your code and I'm not going to because this can be explained in general terms.

    If you use a For loop and process an item directly inside the loop then the items will be processed in series. That means one item must finish processing before the next item starts processing. If you have 10 items and each item takes 1 minute to process, the whole thing takes 10 minutes. If you use some sort of parallelism, of which there are multiple options, then a best-case scenario means that all 10 items get processed at the same time and the whole process takes 1 minute, i.e. a tenfold increase in performance.

    That said, you're almost never going to get that best-case. The reality is that there will be resources that must be shared by the tasks. That may be data or hardware or both. In that case, each thread will have to spend some time waiting for one or more other threads to release those resources and that will slow down the overall performance. In a worst-case scenario, if processing an item uses 100% of processor resources and there is only one single-core processor on the system then only one thread can ever be executed at any one time. As such you will see no benefit from multi-threading and, in fact, you will see a decrease in performance because there is the added overhead of switching between threads.

    So, what this means is that you first need to evaluate what your code does and what hardware it is likely to be run on to decide whether or not you are likely to get a boost from parallelism. These days, it's highly likely that your app will be run on a system that has at least 2 processor cores and maybe more. As such, if there is any part of the processing that doesn't share resources then you will likely get at least some performance gain. If there is some sharing of data between tasks then you may also have to add some synchronisation code, which adds complexity and therefore makes the whole thing harder and more error-prone.

    Now, if you decide that your task can benefit from parallelism, you then need to consider the implementation. In .NET 3.5 or earlier you would use the ThreadPool directly, calling QueueUserWorkItem inside a standard For or For Each loop. In .NET 4.0 or later, you would use the Parallel.For or Parallel.ForEach method and let it worry about the details.

  3. #3
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: Threading with For Loop vs. For Loop only. ( VB.NET 2010 )

    Thanks now i get the concept..

    I just need to ask 1 question, why is there a big difference between..E.G

    Plain loop
    Code:
    for i = 1 to 1000
         label1.text = i
    next i
    in the threading, this is what the code on my threading, this is just 1 thread.

    Code:
    for i = 1 to 1000
         label2.text = i
    next i
    i just process 1 for loop and 1 thread with 1 for loop, with the same amount number to loop, and it just had a big difference.. don't understand why is that.. on my thought "Oh it should be the same, or not to much difference"

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,763

    Re: Threading with For Loop vs. For Loop only. ( VB.NET 2010 )

    I'm not really sure what you're saying. Those two code snippets are exactly the same, other than the Label they refer to, so there's never going to be any difference between how those two code snippets behave.

    What may be a factor is that both those loops access a control and controls should NEVER be access directly on any thread other than the UI thread. Regardless of anything else, if you start a new thread and execute either of those loops on that new thread then you're doing a bad thing and the behaviour is unpredictable. If you're debugging then, by default, doing that would throw an exception. In a release build the code would run but the behaviour may or may not be what you expect. Basically, don't do it. That is simply nothing like a good example of a loop that could be sped up by multithreading. Here's a fairly contrived loop:
    vb.net Code:
    1. Dim chars As New List(Of Char)
    2.  
    3. For Each filePath In IO.Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "*.txt")
    4.     Using reader As New IO.StreamReader(filePath)
    5.         'Discard the first four lines of the file.
    6.         For i = 1 To 4
    7.             reader.ReadLine()
    8.         Next
    9.  
    10.         'Get the fifth line.
    11.         Dim line = reader.ReadLine()
    12.  
    13.         'Get the fifth character and add it to the list.
    14.         chars.Add(line(4))
    15.     End Using
    16. Next
    That is going to open one file at a time. If you could open multiple files and process them simultaneously then you might speed things up:
    vb.net Code:
    1. Dim chars As New ConcurrentBag(Of Char)
    2.  
    3. Parallel.ForEach(IO.Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "*.txt"),
    4.                  Sub(filePath)
    5.                      Using reader As New IO.StreamReader(filePath)
    6.                          'Discard the first four lines of the file.
    7.                          For i = 1 To 4
    8.                              reader.ReadLine()
    9.                          Next
    10.  
    11.                          'Get the fifth line.
    12.                          Dim line = reader.ReadLine()
    13.  
    14.                          'Get the fifth character and add it to the list.
    15.                          chars.Add(line(4))
    16.                      End Using
    17.                  End Sub)
    That is the equivalent code using a parallel For Each loop. Now multiple files can be processed simultaneously so the whole process will likely be faster. You still won't get every file processed at the same time so you won't get the maximum possible improvement. Also, the hard-drive can't read more than one place at a time so there's a physical limitation there. It's also worth noting that the order the items are processed is indeterminate, so you could execute that exact same code multiple times with the exact same files and, while the Chars in the collection would always be the same, their order might be different every time.

  5. #5
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: Threading with For Loop vs. For Loop only. ( VB.NET 2010 )

    Now i understand , thank you so much it almost the same now hehehe.. thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •