Visual Studio 2010 Beta 1 and .AsParallel()

by Mwwhited 13. June 2009 16:06

I decided to play around with VS2010 Beta 1 this weekend. While talking with one of my dev friends, I was asked what I thought about the Parallel extensions. Having not used them yet I figured I'd give them a go. Test Code...


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace ParallelTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int cnt = 100000;
            long p, np;
            var npRng = 0.ToRange(cnt);
            var pRng = 0.ToRange(cnt);

            foreach (var tryCnt in 0.ToRange(10))
            {
                //Base Line
                {
                    var sw = new Stopwatch();
                    sw.Start();

                    var lRng = npRng.Where(i => i.IsPrime()).Count();

                    sw.Stop();
                    np = sw.ElapsedTicks;
                    Console.Write("n:" + np.ToString() + " ");
                }
                //Parallel Test
                {
                    var sw = new Stopwatch();
                    sw.Start();

                    var lRng = pRng.AsParallel().Where(i=>i.IsPrime()).Count();

                    sw.Stop();
                    p = sw.ElapsedTicks;
                    Console.Write("p:" + p.ToString() + " ");
                }

                Console.Write("Try: " + tryCnt.ToString() + "  - ");
                Console.WriteLine(p > np ? "Parallel was slower" : (np == p ? "Same Speeed" : "Parallel was faster"));
            }
            Console.ReadLine();
        }
    }

    public static class Extensions
    {
        public static Range ToRange(this int start, int end = 0, int step = 1)
        {
            return new Range(start, end, step);
        }

        public static bool IsPrime(this int p)
        {
            int upperBound = (int)Math.Sqrt(p);
            var rng = 2.ToRange(upperBound + 1);
            foreach (var i in rng)
                if (p % i == 0) return false;
            return true;
        }
    }

    public class Range : IEnumerable
    {
        public Range(int start = 0, int end = 0, int step = 1)
        {
            this.Start = start;
            this.End = end;
            this.Step = step;
        }

        public int Start { get; private set; }
        public int End { get; private set; }
        public int Step { get; private set; }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            for (int i = this.Start; i < this.End; i += this.Step)
                yield return i;
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
Windows XP SP3, AMD Athlon XP 2700+, 2GB RAM (single core 32bit)

n:2142066 p:2474497 Try: 0  - Parallel was slower
n:2076397 p:2234176 Try: 1  - Parallel was slower
n:1884610 p:2423850 Try: 2  - Parallel was slower
n:1784631 p:2103229 Try: 3  - Parallel was slower
n:1881561 p:1797535 Try: 4  - Parallel was faster
n:1736849 p:1763563 Try: 5  - Parallel was slower
n:1770798 p:1815447 Try: 6  - Parallel was slower
n:1770461 p:1772094 Try: 7  - Parallel was slower
n:1747933 p:1777781 Try: 8  - Parallel was slower
n:1752655 p:1765397 Try: 9  - Parallel was slower
Windows Vista Ultimate, AMD Althon 64 X2 Dual Core 6000+ (3.0 GHz), 4GB RAM (Dual Core 64 bit)

n:3360840 p:3648141 Try: 0  - Parallel was slower
n:4220937 p:1645455 Try: 1  - Parallel was faster
n:3124751 p:1689114 Try: 2  - Parallel was faster
n:3511074 p:1634164 Try: 3  - Parallel was faster
n:3324020 p:1576295 Try: 4  - Parallel was faster
n:3134908 p:1618946 Try: 5  - Parallel was faster
n:3173786 p:1641948 Try: 6  - Parallel was faster
n:3418653 p:1795927 Try: 7  - Parallel was faster
n:3497382 p:1617630 Try: 8  - Parallel was faster
n:2881158 p:1778105 Try: 9  - Parallel was faster

Tags: , , ,

Programming

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

RecentPosts

Badges