示例#1
0
        public static void Start()
        {
            // Create a thread to demonstrate message passing
            var t = new Thread(
                // Specify the source files
                new[]
            {
                // Include this javascript file (since bridge is loaded automatically)
                Thread.GetCurrentJsFileUri()
            }
                );

            // Set a message handler on the thread
            t.OnMessage += o =>
            {
                // Print the message received from the thread
                Script.Call("console.log", "Got message in main thread: ", o);
            };

            // Send a message to the worker
            t.PostMessage("hello from main thread:)");

            Window.SetTimeout(BenchmarkPrimes, 500);
        }
示例#2
0
        // Runs the next primes benchmark
        private static void BenchmarkPrimes()
        {
            // Get the number of threads for this benchmark
            var numberOfThreads = NumberOfThreadsPerTest[0];

            // Remove this number of threads from the available runs
            NumberOfThreadsPerTest.RemoveAt(0);

            Console.WriteLine("Starting next benchmark with " + numberOfThreads + " thread(s)...");

            // Get the start time
            var startTime = DateTime.Now;

            // Create an array to store the prime numbers
            var primeNumbers = new List <int>();
            // Calculate how many ints each thread will check
            var countPerThread = MaxNumber / numberOfThreads;
            // Create an array to store the created threads
            var threads = new List <Thread>();

            // Spawn the threads
            for (var i = 0; i < numberOfThreads; i++)
            {
                // Create the range for this thread
                var range = new PrimeRange
                {
                    First = i * countPerThread + 1,
                    Last  = i * countPerThread + countPerThread + 1
                };

                // Create the new thread
                var t = new Thread(
                    // Specify the source files
                    new[]
                {
                    // Include this javascript file (since bridge is loaded automatically)
                    Thread.GetCurrentJsFileUri()
                }
                    );

                // Start the thread
                t.Start(
                    // Set the entry point to RunPrime
                    RunPrime,
                    // range is the parameter sent to the entry point
                    range,
                    // Set the completion callback when the thread returns a value (thread, original parameter, result)
                    (thread, param, result) =>
                {
                    // Cast the result object to an int array
                    var resultPrimes = result as int[];
                    // Add the primes to the list of primes
                    primeNumbers.AddRange(resultPrimes);
                }
                    );

                // Add the created thread to the list of spawned threads
                threads.Add(t);
            }

            // Wait for all threads to join
            JoinAll(threads, () =>
            {
                // Get the end time
                var endTime = DateTime.Now;

                // Clean up all created threads
                threads.ForEach(e => e.Dispose());

                // Get the number of prime numbers found
                var primeNumberCount = primeNumbers.Count;

                if (PrintFirst1000Primes)
                {
                    // Need to sort the primes array, since it is not in order
                    primeNumbers = primeNumbers.Slice(0, 1000);
                    primeNumbers.Sort();
                    // Print 100 rows of 10 primes
                    for (var i = 0; i < 100; i++)
                    {
                        var s = "";
                        for (var j = 0; j < 10; j++)
                        {
                            s += primeNumbers[i * 10 + j] + " ";
                        }

                        Console.WriteLine(s);
                    }
                }

                // Report the statistics from this run
                Console.WriteLine("Max number: " + MaxNumber + ", Threads: " + numberOfThreads + ", Time taken: " +
                                  (endTime - startTime).TotalMilliseconds + "ms, Number of primes: " +
                                  primeNumberCount);

                // Check if there are any more runs to do
                if (NumberOfThreadsPerTest.Count > 0)
                {
                    // Yes, run the next benchmark
                    BenchmarkPrimes();
                }
                else
                {
                    // Run the baseline benchmark
                    RunBenchmarkOnMainThread();

                    // All done
                    Console.WriteLine("Complete.");
                }
            });
        }