Home > Computer Programming > single thread vs parallel thread

single thread vs parallel thread


hi guys,

i will show you the differences between single thread and parallel thread

lets say we have this function

for(int i=0;i<350000000;i++)

(get real, 350millions counter on the same script, yes, we do that)

what would happen if this function done in 10 simultaneous times in the same process?

we do have this picture as the result

single thread

it takes 1.33 sec each to process the code, and total has 13.3 sec to process 10x the code

this the real codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Action<Action> measure = (body) =>
            {
                var startTime = DateTime.Now;
                body();
                Console.WriteLine("{0} {1}", DateTime.Now - startTime,
                    Thread.CurrentThread.ManagedThreadId);
            };

            Action calcJob = () => {for (int i = 0; i<350000000;i++);};

            Console.WriteLine("Single thread");
            measure(() =>
                {
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                }
            );

            Console.Read();
        }
    }
}

so what we do first is, we create a measure function, so we can measure how does it takes to load, and the process is simple by added 10 x calcJob on the next step

the result is same on the picture,

what we gonna do is, what would happen if we’re use a parallel concept on the same code, what is the effects?

lets think on the parallel concept, the same code is executed on the same processors, if the processors is busy, the operating system allocated the others processor to process it,

same as the concept, while the first code is being process by the processor, the second code is processed by the other processor, I do have a laptop, i3 core processor, it has 4 core, so the third code processed by the third processor, and so on

let see the result:

parallel thread

now, you see on the picture, the total time takes 10x process the code it is only 4.4 second compares to 13.3 second by using the single thread

you can see on the above picture, the thread number 11, 12,etc means PID, the process that done on the processor, it takes the different processor to process the code,

see the first screen, the first only takes 1 PID that PID number 9. so it takes the same processor to process the code, second code, third code, etc

let see the compares head to head

4-single-thread-vs-parallel-thread

well?

this the compares code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Action<Action> measure = (body) =>
            {
                var startTime = DateTime.Now;
                body();
                Console.WriteLine("{0} {1}", DateTime.Now - startTime,
                    Thread.CurrentThread.ManagedThreadId);
            };

            Action calcJob = () => {for (int i = 0; i<350000000;i++);};

            Console.WriteLine("Single thread");
            measure(() =>
                {
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                    measure(calcJob);
                }
            );

            Console.WriteLine("\nPararel thread");
            measure(() =>
                {
                    ParallelEnumerable.Range(1, 10)
                        .WithDegreeOfParallelism(10)
                        .ForAll(_ => measure(calcJob));
                }
            );

            Console.Read();
        }
    }
}

Categories: Computer Programming
  1. No comments yet.
  1. No trackbacks yet.

Please kindly Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: