Facebook
From Filip Wicha, 5 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 264
  1. using System;
  2. using System.Threading;
  3. using System.Diagnostics;
  4.  
  5. namespace _1
  6. {
  7.     class Program
  8.     {
  9.         static void f()
  10.         {
  11.             int x = 0;
  12.             x++;
  13.         }
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             int TimeToCreate = 0;
  18.             int TimeToTerminate = 0;
  19.  
  20.             Stopwatch CreationTime = new Stopwatch();
  21.             Stopwatch TerminationTime = new Stopwatch();
  22.  
  23.             for (int i = 0; i < 100; i++)
  24.             {
  25.                 Thread t1 = new Thread(f);
  26.  
  27.                 //Stopwatch 1
  28.                 CreationTime.Start();
  29.                 t1.Start();
  30.                 CreationTime.Stop();
  31.  
  32.                 //Stopwatch 2
  33.                 TerminationTime.Start();
  34.                 t1.Abort();
  35.                 TerminationTime.Stop();
  36.             }
  37.  
  38.             TimeToCreate = Convert.ToInt32(CreationTime.ElapsedMilliseconds)/100;
  39.             TimeToTerminate = Convert.ToInt32(TerminationTime.ElapsedMilliseconds) / 100;
  40.  
  41.             Console.WriteLine("Creation and terminate time counter for 100 threads\n");
  42.             Console.WriteLine("Average time of a creation of a thread:    " + TimeToCreate + "   ms");
  43.             Console.WriteLine("Average time of a termination of a thread:  " + TimeToTerminate + "    ms");
  44.             Console.ReadLine();
  45.         }
  46.     }
  47. }
  48.