Click here to Skip to main content
15,897,371 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionAnyone know how you can create a screen that looks like the Outlook calendar 2007 Pin
RadyShishi12-Mar-10 1:38
RadyShishi12-Mar-10 1:38 
AnswerRe: Anyone know how you can create a screen that looks like the Outlook calendar 2007 Pin
Eddy Vluggen12-Mar-10 2:26
professionalEddy Vluggen12-Mar-10 2:26 
Questionparallel processing Pin
Tiju John11-Mar-10 23:31
Tiju John11-Mar-10 23:31 
AnswerRe: parallel processing Pin
Eddy Vluggen12-Mar-10 1:01
professionalEddy Vluggen12-Mar-10 1:01 
GeneralRe: parallel processing Pin
Tiju John12-Mar-10 3:16
Tiju John12-Mar-10 3:16 
AnswerRe: parallel processing Pin
Dave Kreskowiak12-Mar-10 2:00
mveDave Kreskowiak12-Mar-10 2:00 
GeneralRe: parallel processing Pin
Tiju John12-Mar-10 3:20
Tiju John12-Mar-10 3:20 
GeneralRe: parallel processing Pin
Dave Kreskowiak12-Mar-10 5:09
mveDave Kreskowiak12-Mar-10 5:09 
Using your code as a base, this is how I would rewrite it. Note I threw together the custom IEnumerator<int> for an enumerable ForStep. I didn't see a "steppable" version in Enumerable or Parallel anywhere, so I threw together my own. Just a reminder, these LINQ queries are not executed until the variables Prime1 and Prime3 are used.

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

namespace IsPrime
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();

            var Primes1 = from x in EnumerableForEach.ForEach(1, 10000000, 4).AsParallel()
                          where IsPrime(x)
                          select x;

            var Primes3 = from x in EnumerableForEach.ForEach(3, 10000000, 4).AsParallel()
                          where IsPrime(x)
                          select x;

            sw.Start();
            Console.WriteLine("Primes (1, step 4) collection size: {0}", Primes1.Count());
            sw.Stop();
            Console.WriteLine("Time: {0}\n", sw.Elapsed);

            sw = Stopwatch.StartNew();
            Console.WriteLine("Primes (3, step 4) collection size: {0}", Primes3.Count());
            sw.Stop();
            Console.WriteLine("Time: {0}", sw.Elapsed);
        }

        static bool IsPrime(int x)
        {
            if (x < 2) return false;

            double sqrRoot = Math.Ceiling(Math.Sqrt(x));
            bool isPrime = true;

            for (int i = 2; i < sqrRoot; i++)
            {
                if (x % i == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            return isPrime;
        }
    }
}

public class EnumerableForEach
{
    public static IEnumerable<int> ForEach(int start, int limit, int step)
    {
        return new EnumerableForEach.ForEachIterator(start, limit, step);
    }


    private sealed class ForEachIterator: IEnumerable<int>, IEnumerator<int>, IDisposable
    {
        int _start;
        int _limit;
        int _step;
        int _current;
    
        public ForEachIterator(int start, int limit, int step)
        {
            _start = start;
            _limit = limit;
            _step = step;
            _current = start - step;
        }

        public IEnumerator<int> GetEnumerator()
        {
            return this;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this;
        }

        public int Current()
        {
            return _current;
        }

        public void Dispose()
        {
            
        }

        object IEnumerator.Current
        {
            get { return _current; }
        }

        public bool MoveNext()
        {
            _current += _step;
            return (_current < _limit);
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }

        int IEnumerator<int>.Current
        {
            get { return _current; }
        }
    }
}

The results I got on a 2 core VM (VMWare) running Windows Server 2003, through 10,000,000 instead of 100,000. Remove the .AsParallel() lines to force these LINQ's to run single threaded.
Primes (1, step 4) collection size: 332625
Time: 00:00:05.4500960
 
Primes (3, step 4) collection size: 332398
Time: 00:00:05.1003926
Press any key to continue . . .

A guide to posting questions on CodeProject[^]



Dave Kreskowiak
Microsoft MVP
Visual Developer - Visual Basic
     2006, 2007, 2008
But no longer in 2009...




AnswerRe: parallel processing Pin
frito_burrito12-Mar-10 4:39
frito_burrito12-Mar-10 4:39 
GeneralRe: parallel processing Pin
harold aptroot12-Mar-10 5:15
harold aptroot12-Mar-10 5:15 
GeneralRe: parallel processing Pin
Luc Pattyn12-Mar-10 6:15
sitebuilderLuc Pattyn12-Mar-10 6:15 
GeneralRe: parallel processing Pin
harold aptroot12-Mar-10 6:35
harold aptroot12-Mar-10 6:35 
GeneralRe: parallel processing Pin
Luc Pattyn12-Mar-10 6:49
sitebuilderLuc Pattyn12-Mar-10 6:49 
GeneralRe: parallel processing Pin
harold aptroot12-Mar-10 7:04
harold aptroot12-Mar-10 7:04 
GeneralRe: parallel processing Pin
Luc Pattyn12-Mar-10 7:43
sitebuilderLuc Pattyn12-Mar-10 7:43 
GeneralRe: parallel processing Pin
harold aptroot12-Mar-10 7:47
harold aptroot12-Mar-10 7:47 
GeneralRe: parallel processing Pin
Luc Pattyn12-Mar-10 7:53
sitebuilderLuc Pattyn12-Mar-10 7:53 
GeneralRe: parallel processing Pin
harold aptroot12-Mar-10 8:01
harold aptroot12-Mar-10 8:01 
GeneralRe: parallel processing Pin
Dave Kreskowiak12-Mar-10 8:09
mveDave Kreskowiak12-Mar-10 8:09 
GeneralRe: parallel processing Pin
Luc Pattyn12-Mar-10 8:37
sitebuilderLuc Pattyn12-Mar-10 8:37 
GeneralRe: parallel processing Pin
Dave Kreskowiak12-Mar-10 8:51
mveDave Kreskowiak12-Mar-10 8:51 
GeneralRe: parallel processing Pin
Luc Pattyn12-Mar-10 9:19
sitebuilderLuc Pattyn12-Mar-10 9:19 
Question[vb.net 2008] How get mp3 title,author and lenght ? Pin
nRush11-Mar-10 21:52
nRush11-Mar-10 21:52 
AnswerRe: [vb.net 2008] How get mp3 title,author and lenght ? Pin
Giorgi Dalakishvili11-Mar-10 22:14
mentorGiorgi Dalakishvili11-Mar-10 22:14 
GeneralRe: [vb.net 2008] How get mp3 title,author and lenght ? Pin
nRush11-Mar-10 23:30
nRush11-Mar-10 23:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.