Guest User

Untitled

a guest
May 11th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Timers;
  9.  
  10. namespace CSharpConcurrency
  11. {
  12.     class Program
  13.     {
  14.         static StreamWriter individualCycles = new StreamWriter("./individual.txt");
  15.         static StreamWriter overAll = new StreamWriter("./overall.txt");
  16.         static StreamWriter finalCalcs = new StreamWriter("./finalCalcs.txt");
  17.         static StreamWriter output = new StreamWriter("./output.txt");
  18.         static void Main(string[] args)
  19.         {
  20.            
  21.  
  22.             #region TPL
  23.            
  24.             for (int i = 0; i < 10; i++)
  25.             {
  26.  
  27.  
  28.                 Console.WriteLine(DateTime.Now);
  29.                 List<long> toCalculate = new List<long> {
  30.                 13548153810,
  31.                 24265915380,
  32.                 35143815420,
  33.                 41452967940,
  34.                 52725472560,
  35.                 63547825480,
  36.                 74936756240, };
  37.                 Stopwatch clock = new Stopwatch();
  38.                 clock.Start();
  39.                 var results = toCalculate.AsParallel().Select(CalculatePrimes).ToList(); //Csharp TPL+LINQ magic -- Selects each long then runs CalculatePrimes on it, puts each returned result into a list matching the id of the longs list.
  40.                 clock.Stop();
  41.                 overAll.WriteLine(clock.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L)));
  42.                 int minNum, maxNum;
  43.                 long smallest, largest;
  44.                 clock.Restart();
  45.                 minNum = results.Min(x => x.Count);
  46.                 maxNum = results.Max(x => x.Count);
  47.                 smallest = results.Min(x => x.Min());
  48.                 largest = results.Max(x => x.Max());
  49.                 clock.Stop();
  50.                 finalCalcs.WriteLine(clock.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L)));
  51.  
  52.                 clock.Restart();
  53.                 //Tested by comparing results
  54.                 Console.WriteLine("Smallest Number Of Prime Factors: " + minNum);
  55.                 Console.WriteLine("Largest Number Of Prime Factors: " + maxNum);
  56.                 Console.WriteLine("Smallest Prime Factor :" + smallest);
  57.                 Console.WriteLine("Largest Prime Factor :" + largest);
  58.                 clock.Stop();
  59.                 output.WriteLine(clock.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L)));
  60.                 Console.WriteLine(DateTime.Now);
  61.  
  62.                 individualCycles.Flush();
  63.                 overAll.Flush();
  64.                 finalCalcs.Flush();
  65.                 output.Flush();
  66.  
  67.             }
  68.             Console.WriteLine("COMPLETELY FINISHED. PRESS ENTER TO EXIT");
  69.             Console.ReadKey();
  70.  
  71.  
  72.         }
  73.        
  74.  
  75.             #endregion
  76.  
  77.  
  78.  
  79.        
  80.         static List<long> CalculatePrimes(long number)
  81.         {
  82.             Stopwatch clock = new Stopwatch();
  83.             clock.Start();
  84.             List<long> primeNumbers = new List<long>();
  85.             for (int i = 2; i < number; i++)
  86.             {
  87.                 while (number % i == 0)
  88.                 {
  89.                     primeNumbers.Add(i);
  90.                     number /= i;
  91.                 }
  92.             }
  93.             clock.Stop();
  94.             individualCycles.WriteLine(clock.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L)));
  95.             Console.WriteLine("Completed");
  96.             return primeNumbers;
  97.         }
  98.  
  99.        
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment