Advertisement
Guest User

LINQ/PLINQ Experiment

a guest
Aug 9th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace LINQTests
  9. {
  10.   class Program
  11.   {
  12.     static void Main( string[] args )
  13.     {
  14.       Console.WriteLine( "How many dice to roll? " );
  15.       int NDice = Convert.ToInt32( Console.ReadLine() );
  16.  
  17.       Console.WriteLine( "How many sides per die? " );
  18.       int NSides = Convert.ToInt32( Console.ReadLine() );
  19.  
  20.       Console.WriteLine( "Modifier? " );
  21.       int Modifier = Convert.ToInt32( Console.ReadLine() );
  22.  
  23.       Console.WriteLine( "Target total? " );
  24.       int Target = Convert.ToInt32( Console.ReadLine() );
  25.  
  26.       Console.WriteLine( "====================" );
  27.  
  28.       DoRollsSequential( NDice, NSides, Modifier, Target );
  29.  
  30.       Console.WriteLine( "====================" );
  31.  
  32.       DoRollsParallel( NDice, NSides, Modifier, Target );
  33.  
  34.       Console.WriteLine( "====================" );
  35.  
  36.       Console.WriteLine( "Please press <ENTER> to continue..." );
  37.       string thing = Console.ReadLine();
  38.     }
  39.  
  40.     public static void DoRollsSequential( int NDice, int NSides, int Modifier, int Target )
  41.     {
  42.       Console.WriteLine( "Generating rolls sequentially..." );
  43.  
  44.       List<int> Rolls = new List<int>();
  45.       Stopwatch Watch = Stopwatch.StartNew();
  46.  
  47.       for ( int i = 0; i < NDice; i++ )
  48.       {
  49.         Rolls.Add( RollDie( NSides, Modifier ) );
  50.       }
  51.  
  52.       Watch.Stop();
  53.       Console.WriteLine( "Rolls took {0} ms to generate.", Watch.ElapsedMilliseconds );
  54.       Watch.Reset();
  55.  
  56.       Console.WriteLine( "Retrieving successful rolls sequentially..." );
  57.  
  58.       Watch = Stopwatch.StartNew();
  59.       List<int> Success = DoLINQ( Rolls, Target );
  60.       Watch.Stop();
  61.  
  62.       Console.WriteLine( "Filtering took {0} ms.", Watch.ElapsedMilliseconds );
  63.       Console.WriteLine( "There were {0} successes.", Success.Count );
  64.     }
  65.  
  66.     public static void DoRollsParallel( int NDice, int NSides, int Modifier, int Target )
  67.     {
  68.       Console.WriteLine( "Generating rolls in parallel..." );
  69.  
  70.       List<int> Rolls = new List<int>();
  71.       Stopwatch Watch = Stopwatch.StartNew();
  72.  
  73.       Parallel.For( 0, NDice, i =>
  74.         {
  75.           Rolls.Add( RollDie( NSides, Modifier ) );
  76.         } );
  77.  
  78.       Watch.Stop();
  79.       Console.WriteLine( "Rolls took {0} ms to generate.", Watch.ElapsedMilliseconds );
  80.       Watch.Reset();
  81.  
  82.       Console.WriteLine( "Retrieving successful rolls in parallel..." );
  83.  
  84.       Watch = Stopwatch.StartNew();
  85.       List<int> Success = DoPLINQ( Rolls, Target );
  86.       Watch.Stop();
  87.  
  88.       Console.WriteLine( "Filtering took {0} ms.", Watch.ElapsedMilliseconds );
  89.       Console.WriteLine( "There were {0} successes.", Success.Count );
  90.     }
  91.  
  92.     public static int RollDie( int NSides, int Modifier )
  93.     {
  94.       Random rand = new Random( DateTime.Now.Millisecond );
  95.  
  96.       return ( rand.Next( 1, NSides + 1 ) + Modifier );
  97.     }
  98.  
  99.     public static List<int> DoLINQ( List<int> Rolls, int Target )
  100.     {
  101.       /*return Rolls
  102.         .Where( num => num >= Target )
  103.         .ToList();*/
  104.       return ( from   num in Rolls
  105.                where  num >= Target
  106.                select num ).ToList();
  107.     }
  108.  
  109.     public static List<int> DoPLINQ( List<int> Rolls, int Target )
  110.     {
  111.       /*return Rolls.AsParallel()
  112.         .Where( num => num >= Target )
  113.         .ToList();*/
  114.       return ( from   num in Rolls.AsParallel()
  115.                where  num >= Target
  116.                select num ).ToList();
  117.     }
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement