Darw1n34
Darw1n34

Reputation: 332

Parallel.For vs for loop

I am wrapping my head around running paralleling threads, so I decided to test it out on a smaller basis and then expand when I am comfortable. I am pitting the same process against itself; one using Parallel.For and the other using a basic for loop. I am capturing the time (in ticks) for the comparison. The example code takes an array (in this case 53 two character strings) and fills a given ListBox with the array.

What makes little to no sense to me is that when I run the basic For loop, it results in an average of 1,400 ticks, but when I run the Parallel.For loop it returns 5,200 ticks on average. Is the sample size too small for the parallel to be effective?

Here are the two snippets I am using. The Parallel.For loop is:

public void ListboxFromArray(ListBox listbox, string[] array1)
{
    // This method takes an array and fills a listbox full of items from the array
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();

    Parallel.For(0, array1.Count(),
    index =>
    {
        listbox.Items.Add(array1[index]);
    });
    stopWatch.Stop();
    long ts = stopWatch.ElapsedTicks;
    string elapsedTime = ts.ToString() + " Ticks"; ;
    MessageBox.Show(elapsedTime);

}

and the for loop is:

public void ListboxFromArray(ListBox listbox, string[] array1)
{
    // This method takes an array and fills a listbox full of items from the array
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();

    for (int i = 0; i < array1.Count(); i++)
    {
        listbox.Items.Add(array1[i]);
    }
    stopWatch.Stop();
    long ts = stopWatch.ElapsedTicks;
    string elapsedTime = ts.ToString() + " Ticks"; ;
    MessageBox.Show(elapsedTime);
}

Thank you for any input or validation of my thoughts in advance.

Upvotes: 1

Views: 4016

Answers (3)

Mark Brackett
Mark Brackett

Reputation: 85685

I don't think you can do that....first, you're accessing a UI control (ListBox) from a non-UI thread. AFAIK, Parallel.For doesn't do any thread marshalling to make that safe. Second, you're accessing the same collection (ListBox.Items) from multiple threads without locking - again, not safe.

In general, I'd say this is not what Parallel.For is designed for. You're not exactly bottlenecking on I/O or CPU here, which means any parallelism overhead is going to dwarf any conceivable improvement.

Upvotes: 2

Michael Mairegger
Michael Mairegger

Reputation: 7301

Using Parallel.For is useful if you want to compute for each item in the collection something that takes some time amount of time to compute.

int[] coll = new int[]{10,9,8,7,6,5,4,3,2,1};

Parallel.ForEach(coll,
    item=>
    {
       Thread.Sleep(TimeSpan.FromSeconds(item));
       Console.WriteLine(item + "Finished");
    });

compared to the sequential way

foreach (var item in coll)
{
    Thread.Sleep(TimeSpan.FromSeconds(item));
    Console.WriteLine(item + "Finished");
}

the first code runs faster, because it really does the work in parallel.

In your case, if you just want to perform just "no work" then the overhead in thead is just to big.

Upvotes: 2

PhillipH
PhillipH

Reputation: 6222

I think your sample size is way too small, and the work you are doing within the loop is far too small to overcome the overhead of the task/thread management. There is little point in paraleling a in-memory op that consumes so little CPU. If you were doing a 100,000 item sort then maybe...

Upvotes: 1

Related Questions