-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPerformance.cs
61 lines (53 loc) · 1.54 KB
/
ThreadPerformance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace OOPs
{
class ThreadPerformance
{
public static void IncrementCounter1()
{
long Count = 0;
for (long i = 0; i < 1000000000; i++)
{
Count++;
}
Console.WriteLine("IncrteamentCounter1:" + Count);
}
public static void IncrementCounter2()
{
long Count = 0;
for (long i = 0; i < 1000000000; i++)
{
Count++;
}
Console.WriteLine("IncrteamentCounter2:" + Count);
}
static void Main()
{
//First Stopwatch
Stopwatch s1 = new Stopwatch();
s1.Start();
IncrementCounter1();
IncrementCounter2();
s1.Stop();
Thread t1 = new Thread(IncrementCounter1);
Thread t2 = new Thread(IncrementCounter2);
//Second Stopwatch
Stopwatch s2 = new Stopwatch();
s2.Start();
t1.Start();
t2.Start();
s2.Stop();
//Stop primery thread executing before t1 and t2
t1.Join();
t2.Join();
Console.WriteLine("Time 1:" + s1.ElapsedMilliseconds);
Console.WriteLine("Time 2:" + s2.ElapsedMilliseconds);
Console.ReadKey();
}
}
}