代码之家  ›  专栏  ›  技术社区  ›  FIre Panda

线程与并行性能

  •  0
  • FIre Panda  · 技术社区  · 6 年前

    我正在努力理解 螺纹 平行的 . 我创建了两个函数,一个用于并行,另一个用于调用的线程。调用 10线程 似乎是 更快 ,有人能解释一下吗?会 螺纹 使用 多处理器 在系统中可用(并行执行)还是只执行 时间分割 关于clr?

    public static bool ParallelProcess()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        Parallel.For(0, 10, x =>
        {
            Console.WriteLine(string.Format("Printing {0} thread = {1}", x,
                Thread.CurrentThread.ManagedThreadId));
            Thread.Sleep(3000);
        });
        sw.Stop();
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.Seconds));
    
        return true;
    }
    
    public static bool ParallelThread()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        for (int i = 0; i < 10; i++)
        {
            Thread t = new Thread(new ThreadStart(Thread1));
            t.Start();
            if (i == 9)
                t.Join();
        }
        sw.Stop();
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.Seconds));
    
        return true;
    }
    
    private static void Thread1()
    {
        Console.WriteLine(string.Format("Printing {0} thread = {1}", 0,
               Thread.CurrentThread.ManagedThreadId));
        Thread.Sleep(3000);
    }
    

    当在下面的方法中调用时,parallel.for花费了两次时间,然后是线程。

    Algo.ParallelThread(); //took 3 secs
    Algo.ParallelProcess();  //took 6 secs
    
    4 回复  |  直到 6 年前
        1
  •  2
  •   Enigmativity    6 年前

    sw.Elapsed.Seconds int 1 sw.Elapsed.TotalSeconds double

    Parallel.For new Thread(() => ...)

    Thread.Sleep(3000);

    new Thread

    public static bool ParallelProcess()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        Parallel.For(0, 10, x =>
        {
            Console.WriteLine(string.Format("Printing {0} thread = {1}", x, Thread.CurrentThread.ManagedThreadId));
        });
        sw.Stop();
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.TotalMilliseconds));
    
        return true;
    }
    
    public static bool ParallelThread()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        var threads = Enumerable.Range(0, 10).Select(x => new Thread(new ThreadStart(Thread1))).ToList();
        foreach (var thread in threads) thread.Start();
        foreach (var thread in threads) thread.Join();
        sw.Stop();
    
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.TotalMilliseconds));
    
        return true;
    }
    
    private static void Thread1()
    {
        Console.WriteLine(string.Format("Printing {0} thread = {1}", 0, Thread.CurrentThread.ManagedThreadId));  
    }
    

    ParallelProcess();
    ParallelThread();
    ParallelProcess();
    ParallelThread();
    ParallelProcess();
    ParallelThread();   
    

    Time in secs 3.8617
    Time in secs 4.7719
    Time in secs 0.3633
    Time in secs 1.6332
    Time in secs 0.3551
    Time in secs 1.6148
    

        2
  •  2
  •   Saeb Amini    6 年前

    Parallel

    ThreadPool.SetMinThreads(100, 100)

        3
  •  1
  •   Theraot    6 年前

    ParallelThread ParallelProcess

    public static bool ParallelThread()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        var threads = new Thread[10];
        for (int i = 0; i < 10; i++)
        {
            int x = i;
            threads[i] = new Thread(() => Thread1(x));
            threads[i].Start();
        }
        for (int i = 0; i < 10; i++)
        {
            threads[i].Join();
        }
        sw.Stop();
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.Seconds));
    
        return true;
    }
    
    private static void Thread1(int x)
    {
        Console.WriteLine(string.Format("Printing {0} thread = {1}", x,
               Thread.CurrentThread.ManagedThreadId));
        Thread.Sleep(3000);
    }
    

    ThreadPool

    public static bool ParallelThread()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        var events = new ManualResetEvent[10];
            for (int i = 0; i < 10; i++)
        {
            int x = i;
            events[x] = new ManualResetEvent(false);
            ThreadPool.QueueUserWorkItem
                (
                    _ =>
                    {
                        Thread1(x);
                        events[x].Set();
                    }
                );
        }
        for (int i = 0; i < 10; i++)
        {
            events[i].WaitOne();
        }
        sw.Stop();
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.Seconds));
    
        return true;
    }
    
    private static void Thread1(int x)
    {
        Console.WriteLine(string.Format("Printing {0} thread = {1}", x,
               Thread.CurrentThread.ManagedThreadId));
        Thread.Sleep(3000);
    }
    

    WaitAll STAThread


    Thread.Sleep(3000)

    Console.WriteLine

    void Main()
    {
        ParallelThread();
        ParallelProcess();
    }
    
    public static bool ParallelProcess()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        Parallel.For(0, 100, x =>
        {
            /*Console.WriteLine(string.Format("Printing {0} thread = {1}", x,
                Thread.CurrentThread.ManagedThreadId));*/
            Thread.Sleep(3000);
        });
        sw.Stop();
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.Seconds));
    
        return true;
    }
    
    public static bool ParallelThread()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        var events = new ManualResetEvent[100];
            for (int i = 0; i < 100; i++)
        {
            int x = i;
            events[x] = new ManualResetEvent(false);
            ThreadPool.QueueUserWorkItem
                (
                    _ =>
                    {
                        Thread1(x);
                        events[x].Set();
                    }
                );
        }
        for (int i = 0; i < 100; i++)
        {
            events[i].WaitOne();
        }
        sw.Stop();
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.Seconds));
    
        return true;
    }
    
    private static void Thread1(int x)
    {
        /*Console.WriteLine(string.Format("Printing {0} thread = {1}", x,
               Thread.CurrentThread.ManagedThreadId));*/
        Thread.Sleep(3000);
    }
    

    ThreadPool.SetMinThreads(100, 100);

    public static bool ParallelThread()
    {
        Stopwatch sw = new Stopwatch();
    
        sw.Start();
        var threads = new Thread[100];
        for (int i = 0; i < 100; i++)
        {
            int x = i;
            threads[i] = new Thread(() => Thread1(x));
            threads[i].Start();
        }
        for (int i = 0; i < 100; i++)
        {
            threads[i].Join();
        }
        sw.Stop();
        Console.WriteLine(string.Format("Time in secs {0}", sw.Elapsed.Seconds));
    
        return true;
    }
    
    private static void Thread1(int x)
    {
        /*Console.WriteLine(string.Format("Printing {0} thread = {1}", x,
               Thread.CurrentThread.ManagedThreadId));*/
        Thread.Sleep(3000);
    }
    

    Parallel.For ThreadPool.SetMaxThreads(1, 1);


    ProcessThread.ProcessorAffinity fibers

        4
  •  -1
  •   user1451111    6 年前

    Thread Parallel.For