private static double WritePiBenchMark(int piDigit, int piIteration) { var iteration = 100; var miliSecs = new long[iteration]; var sw1 = Stopwatch.StartNew(); for (var i = 0; i < iteration; i++) { sw1.Start(); var calculator = new PiCalculator(); var pi = calculator.GetPi(piDigit, piIteration); sw1.Stop(); miliSecs[i] = sw1.ElapsedMilliseconds; sw1.Reset(); } var average = miliSecs.Average(); Console.WriteLine("Pi digit\t\tPi Iteration\t\tAvgCalc Milliseconds"); Console.WriteLine($"{piDigit}\t\t\t{piIteration}\t\t\t{average}"); return(average); }
public static void ChapterMain() { Task <string> task = Task.Factory.StartNew <string>( () => PiCalculator.Calculate(10)); Task faultedTask = task.ContinueWith( (antecedentTask) => { Trace.Assert(task.IsFaulted); Console.WriteLine("Task State: Faulted"); }, TaskContinuationOptions.OnlyOnFaulted); Task canceledTask = task.ContinueWith( (antecedentTask) => { Trace.Assert(task.IsCanceled); Console.WriteLine("Task State: Canceled"); }, TaskContinuationOptions.OnlyOnCanceled); Task completedTask = task.ContinueWith( (antecedentTask) => { Trace.Assert(task.IsCompleted); Console.WriteLine("Task State: Completed"); }, TaskContinuationOptions.OnlyOnRanToCompletion); completedTask.Wait(); }
public static void Main() { // Use Task.Factory.StartNew<string>() for // TPL prior to .NET 4.5 Task <string> task = Task.Run <string>( () => PiCalculator.Calculate(100)); foreach ( char busySymbol in Utility.BusySymbols()) { if (task.IsCompleted) { Console.Write('\b'); break; } Console.Write(busySymbol); } Console.WriteLine(); Console.WriteLine(task.Result); System.Diagnostics.Trace.Assert( task.IsCompleted); }
private void btnStart_Click(object sender, EventArgs e) { this.barAll.Value = 0; this.barAll.Maximum = (int)numTimes.Value; for (int i = 0; i < numThread.Value; i++) { PiCalculator calculator = null; if (i == numThread.Value - 1) { calculator = new PiCalculator((uint)(numTimes.Value / numThread.Value + (numTimes.Value % numThread.Value)), once, all); } else { calculator = new PiCalculator((uint)(numTimes.Value / numThread.Value), once, all); } threads.Add(new Thread(new ThreadStart(calculator.run))); } start = UInt64.Parse(DateTime.Now.ToFileTimeUtc().ToString()); foreach (var t in threads) { t.Start(); } }
static void Main(string [] args) { try { var param = Validator.GetArgsIntArrFromSrtArr(args); var piCalculator = new PiCalculator { AmountStep = param [0], AmountThread = 4 }; piCalculator.SetSpinCount(Convert.ToInt32(param [1])); var timer = System.Diagnostics.Stopwatch.StartNew(); Console.WriteLine(piCalculator.CalculatePi()); timer.Stop(); Console.WriteLine(timer.ElapsedMilliseconds); timer = System.Diagnostics.Stopwatch.StartNew(); Console.WriteLine(piCalculator.CalculatePi(Convert.ToInt32(param [2]))); timer.Stop(); Console.WriteLine(timer.ElapsedMilliseconds); } catch (Exception e) { Console.WriteLine(e.Message); } }
public static void ChapterMain() { // Use Task.Factory.StartNew<string>() for // TPL prior to .NET 4.5 Task <string> task = Task.Run <string>( () => PiCalculator.Calculate(100)); foreach ( char busySymbol in Utility.BusySymbols()) { if (task.IsCompleted) { Console.Write('\b'); break; } Console.Write(busySymbol); } Console.WriteLine(); Console.WriteLine(task.Result); if (!task.IsCompleted) { throw new Exception("Task Should Be Completed"); } }
static void Main(string[] args) { if (args.Length != 4) { PrintHelp(); return; } try { for (int i = 0; i < args.Length; i++) { if (args[i].Contains("t")) { threadCount = uint.Parse(args[i + 1]); } else if (args[i].Contains("c")) { cycleCount = uint.Parse(args[i + 1]); } } } catch (Exception ex) { Console.WriteLine("Invalid arguments"); PrintHelp(); return; } if (threadCount < 1 || cycleCount < 100) { PrintHelp(); return; } for (int i = 0; i < threadCount; i++) { PiCalculator calculator = null; if (i == threadCount - 1) { calculator = new PiCalculator((uint)(cycleCount / threadCount + (cycleCount % threadCount)), Once, AllFinished); } else { calculator = new PiCalculator((uint)(cycleCount / threadCount), Once, AllFinished); } threads.Add(new Thread(new ThreadStart(calculator.run))); } Console.WriteLine($"start {cycleCount} calculation(s) on {threadCount} thread(s)"); start = UInt64.Parse(DateTime.Now.ToFileTimeUtc().ToString()); foreach (var t in threads) { t.Start(); } reset.WaitOne(); }
private void CalculatePi(IAsyncAction operation) { if (isStopped) { return; } string result = PiCalculator.Calculate(PI_DIGITS); Debug.WriteLine(result); }
public static void ChapterMain() { string pi = null; const int iterations = TotalDigits / BatchSize; for (int i = 0; i < iterations; i++) { pi += PiCalculator.Calculate( BatchSize, i * BatchSize); } Console.WriteLine(pi); }
public static void ChapterMain() { string pi = null; int iterations = TotalDigits / BatchSize; string[] sections = new string[iterations]; Parallel.For(0, iterations, (i) => { sections[i] += PiCalculator.Calculate( BatchSize, i * BatchSize); }); pi = string.Join("", sections); Console.WriteLine(pi); }
public static void Main() { string pi = null; const int iterations = TotalDigits / BatchSize; string[] sections = new string[iterations]; //TPL提供了一个辅助方法,并行迭代 Parallel.For(0, iterations, (i) => { sections[i] = PiCalculator.Calculate(BatchSize, i * BatchSize); }); pi = string.Join("", sections); Console.WriteLine(pi); }
private static void WritePi( CancellationToken cancellationToken) { const int batchSize = 1; string piSection = string.Empty; int i = 0; while (!cancellationToken.IsCancellationRequested || i == int.MaxValue) { piSection = PiCalculator.Calculate( batchSize, (i++) * batchSize); Console.Write(piSection); } }
protected override void OnReceive(object message) { if (message is CalcOptions options) { var calculator = new PiCalculator(); var pi = calculator.GetPi(options.Digits, options.Iterations); var strPi = pi.ToString(); var actorSelection = Context.ActorSelection(options.ReceiverAddress); actorSelection.Tell(new PiNumber { Pi = strPi }); } }
public static void ChapterMain() { // Use Task.Factory.StartNew<string>() for // TPL prior to .NET 4.5 Task <string> task = Task.Run <string>( () => PiCalculator.Calculate(10)); Task faultedTask = task.ContinueWith( (antecedentTask) => { if (!antecedentTask.IsFaulted) { throw new Exception("Antecedent Task Should Be Faulted"); } Console.WriteLine( "Task State: Faulted"); }, TaskContinuationOptions.OnlyOnFaulted); Task canceledTask = task.ContinueWith( (antecedentTask) => { if (!antecedentTask.IsCanceled) { throw new Exception("Antecedent Task Should Be Canceled"); } Console.WriteLine( "Task State: Canceled"); }, TaskContinuationOptions.OnlyOnCanceled); Task completedTask = task.ContinueWith( (antecedentTask) => { if (!antecedentTask.IsCompleted) { throw new Exception("Antecedent Task Should Be Completed"); } Console.WriteLine( "Task State: Completed"); }, TaskContinuationOptions. OnlyOnRanToCompletion); completedTask.Wait(); }
public Task ReceiveAsync(IContext context) { if (context.Message is CalcOptions options) { var calculator = new PiCalculator(); var pi = calculator.GetPi(options.Digits, options.Iterations); var strPi = pi.ToString(); PID echoActor = new PID(options.ReceiverAddress, "echoActor"); echoActor.Tell(new PiNumber { Pi = strPi }); } return(Actor.Done); }
public void CalculateAsync <TState>( int digits, CancellationToken cancelToken, TState userState) { Task <string> .Factory.StartNew( () => PiCalculator.Calculate(digits), cancelToken) .ContinueWith <string>( continueTask => { CalculateCompleted(typeof(PiCalculator), new CalculateCompletedEventArgs( continueTask.Result, continueTask.Exception, cancelToken.IsCancellationRequested, userState)); return(continueTask.Result); }); }
public void CalculateAsync <TState>( int digits, CancellationToken cancelToken = default(CancellationToken), TState userState = default(TState)) { SynchronizationContext. SetSynchronizationContext( AsyncOperationManager. SynchronizationContext); // Ensure the continuation runs on the current // thread, and that therefore the event will // be raised on the same thread that called // this method in the first place. TaskScheduler scheduler = TaskScheduler. FromCurrentSynchronizationContext(); Task.Run( () => { return(PiCalculator.Calculate(digits)); }, cancelToken) .ContinueWith( continueTask => { Exception exception = continueTask.Exception == null ? continueTask.Exception : continueTask.Exception. InnerException; CalculateCompleted( typeof(PiCalculator), new CalculateCompletedEventArgs( continueTask.Result, exception, cancelToken.IsCancellationRequested, userState)); }, scheduler); }
public static void Main() { Task <string> task = Task.Factory.StartNew <string>( () => PiCalculator.Calculate(100)); foreach (char busySymbol in Utility.BusySymbols()) { if (task.IsCompleted) { Console.Write('\b'); break; } Console.Write(busySymbol); } Console.WriteLine(); // Blocks until task completes. Console.WriteLine(task.Result); Trace.Assert(task.IsCompleted); }
static void Main(string[] args) { Task <string> task = Task.Run <string>(() => PiCalculator.Calculate(100000)); foreach (var busySymbol in Utility.BusySymbols()) { if (task.IsCompleted) { Console.WriteLine('\b'); break; } Console.WriteLine(busySymbol);//太快的话,这会被忽略 } Console.WriteLine(); Console.WriteLine(task.Result); //会阻塞调用任务室外线程 System.Diagnostics.Trace.Assert(task.IsCompleted); //判断task.IsCompleted真假,false则出现弹框 }
public void TestInitialize() { piCalculator = new PiCalculator(); }
public void TestCleanup() { piCalculator = null; }
public async Task <IActionResult> Post([FromBody] int digits) { var pi = await Task.Run(() => PiCalculator.GetPi(digits)); return(Ok(pi)); }