Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LINQTests
- {
- class Program
- {
- static void Main( string[] args )
- {
- Console.WriteLine( "How many dice to roll? " );
- int NDice = Convert.ToInt32( Console.ReadLine() );
- Console.WriteLine( "How many sides per die? " );
- int NSides = Convert.ToInt32( Console.ReadLine() );
- Console.WriteLine( "Modifier? " );
- int Modifier = Convert.ToInt32( Console.ReadLine() );
- Console.WriteLine( "Target total? " );
- int Target = Convert.ToInt32( Console.ReadLine() );
- Console.WriteLine( "====================" );
- DoRollsSequential( NDice, NSides, Modifier, Target );
- Console.WriteLine( "====================" );
- DoRollsParallel( NDice, NSides, Modifier, Target );
- Console.WriteLine( "====================" );
- Console.WriteLine( "Please press <ENTER> to continue..." );
- string thing = Console.ReadLine();
- }
- public static void DoRollsSequential( int NDice, int NSides, int Modifier, int Target )
- {
- Console.WriteLine( "Generating rolls sequentially..." );
- List<int> Rolls = new List<int>();
- Stopwatch Watch = Stopwatch.StartNew();
- for ( int i = 0; i < NDice; i++ )
- {
- Rolls.Add( RollDie( NSides, Modifier ) );
- }
- Watch.Stop();
- Console.WriteLine( "Rolls took {0} ms to generate.", Watch.ElapsedMilliseconds );
- Watch.Reset();
- Console.WriteLine( "Retrieving successful rolls sequentially..." );
- Watch = Stopwatch.StartNew();
- List<int> Success = DoLINQ( Rolls, Target );
- Watch.Stop();
- Console.WriteLine( "Filtering took {0} ms.", Watch.ElapsedMilliseconds );
- Console.WriteLine( "There were {0} successes.", Success.Count );
- }
- public static void DoRollsParallel( int NDice, int NSides, int Modifier, int Target )
- {
- Console.WriteLine( "Generating rolls in parallel..." );
- List<int> Rolls = new List<int>();
- Stopwatch Watch = Stopwatch.StartNew();
- Parallel.For( 0, NDice, i =>
- {
- Rolls.Add( RollDie( NSides, Modifier ) );
- } );
- Watch.Stop();
- Console.WriteLine( "Rolls took {0} ms to generate.", Watch.ElapsedMilliseconds );
- Watch.Reset();
- Console.WriteLine( "Retrieving successful rolls in parallel..." );
- Watch = Stopwatch.StartNew();
- List<int> Success = DoPLINQ( Rolls, Target );
- Watch.Stop();
- Console.WriteLine( "Filtering took {0} ms.", Watch.ElapsedMilliseconds );
- Console.WriteLine( "There were {0} successes.", Success.Count );
- }
- public static int RollDie( int NSides, int Modifier )
- {
- Random rand = new Random( DateTime.Now.Millisecond );
- return ( rand.Next( 1, NSides + 1 ) + Modifier );
- }
- public static List<int> DoLINQ( List<int> Rolls, int Target )
- {
- /*return Rolls
- .Where( num => num >= Target )
- .ToList();*/
- return ( from num in Rolls
- where num >= Target
- select num ).ToList();
- }
- public static List<int> DoPLINQ( List<int> Rolls, int Target )
- {
- /*return Rolls.AsParallel()
- .Where( num => num >= Target )
- .ToList();*/
- return ( from num in Rolls.AsParallel()
- where num >= Target
- select num ).ToList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement