Monday, January 24, 2011

Using multicore power : TPL library ,a simple test for Parallel.For , running time "< 40%"

Recent pc have multicore processor but rarely code is optimized for using this power,generally processor  allows a fast responsive application without using multitasking multithreading tecniques , some time you have that a single threaded application run into a core and other core  sleep....


I have do a simple test for seeing performance difference from sequential programming and parallel programming "ready to use" from TPL library :
language  c# ,namespace windows forms ,cryptography,system.threading.tasks (under last namespace you can find Parallel and other interesting things like PLinq and Task object ),
like hard work for processor I choose generation of asymettric pairs key I use RSA.Create()

this is the code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace TestParallel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           

        }

        private void TestParallel()
        {
            DateTime dt1 = DateTime.Now;         
            var pairs = new string[100];
            //auto generate multiple thread
            //rsa creation keys like hard work
            Parallel.For(0, pairs.Length,
                          i => pairs[i] = RSA.Create().ToXmlString(true));
            DateTime dt2 = DateTime.Now;
            TimeSpan ts = dt2.Subtract(dt1);
            int hours = ts.Hours;
            int minutes = ts.Minutes;
            int seconds = ts.Seconds;
            int millisesonds = ts.Milliseconds;
            string str = hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString() + ":" + millisesonds.ToString();       
            this.textBox1.Text=str;
        }

        private void TestSequential()
        {
            DateTime dt1 = DateTime.Now;
            //single thread
            var pairs = new string[100];
            int l = pairs.Length;
            int k=0;
            while(k<l) {
                pairs[k] = RSA.Create().ToXmlString(true);
                k++;
            }
            DateTime dt2 = DateTime.Now;
            TimeSpan ts = dt2.Subtract(dt1);
            int hours = ts.Hours;
            int minutes = ts.Minutes;
            int seconds = ts.Seconds;
            int millisesonds = ts.Milliseconds;
            string str = hours.ToString() + ":" + minutes.ToString() + seconds.ToString() + ":" + millisesonds.ToString();
            this.textBox2.Text = str;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.TestParallel();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.TestSequential();
        }

    }
}


and this is the result :


Parallel time execution 13,7 seconds ,sequential execution 23 seconds ,my pc is dual core and advantage is > 40% if you have recent multicore processor you can do a test ,

success of parallel is not a surprise but percentage reveal an excellent performance


0 commenti:

Post a Comment