Exemplo n.º 1
0
        private void ParallelLoopTest()
        {
            ConcurrentBag <PLoopResult> resultBag = new ConcurrentBag <PLoopResult>();
            ParallelLoop pLoop = new ParallelLoop(0, 180, ParallelPriolity.Full, LogThreadID);

            pLoop.RunWait();

            List <PLoopResult> resultList = resultBag.ToList().OrderBy(p => p.fromInclusive).ToList();

            for (int resultI = 0; resultI < resultList.Count; ++resultI)
            {
                Console.WriteLine(resultList[resultI]);
            }

            void LogThreadID(int fromInclusive, int toExclusive)
            {
                resultBag.Add(new PLoopResult(fromInclusive, toExclusive, Thread.CurrentThread.ManagedThreadId));
            }
        }
Exemplo n.º 2
0
        public static FuncProfileResults ProfileFunction(int repeatCount, bool useMultithread, params Action[] actions)
        {
            FuncProfileResult[] results = new FuncProfileResult[actions.Length];

            LoopDelegate profileAction = (int startI, int endI) => {
                for (int actionI = startI; actionI < endI; ++actionI)
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Restart();

                    Action action = actions[actionI];
                    if (action != null)
                    {
                        for (int repeatI = 0; repeatI < repeatCount; ++repeatI)
                        {
                            action();
                        }
                    }

                    watch.Stop();
                    results[actionI] = new FuncProfileResult(action, (float)watch.GetElapsedMilliseconds());
                }
            };

            if (useMultithread)
            {
                ParallelLoop pLoop = new ParallelLoop(0, actions.Length, ParallelPriolity.Full, profileAction);
                pLoop.RunWait();
            }
            else
            {
                profileAction(0, actions.Length);
            }

            return(new FuncProfileResults(results));
        }