Esempio n. 1
0
        private static void getSingleRange()
        {
            Console.Clear();
            Console.WriteLine("Enter input 1");
            int input1 = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter input 2");
            int        input2 = int.Parse(Console.ReadLine());
            PrimeRange res    = gpd.GetBySingleRange(input1, input2);

            Console.WriteLine(res.Input1.ToString() + " " + res.Input2.ToString() + " " + res.NumOfPrimes.ToString() + " " + res.Occurrences.ToString());
            Console.ReadLine();
        }
Esempio n. 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.");
                }
            });
        }