-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter20.cs
145 lines (116 loc) · 4.31 KB
/
Chapter20.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Net;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Chapter20
{
class MyDownloadStringLow
{
Stopwatch sw = new Stopwatch();
public void DoRun()
{
const int LargeNumber = 6000000;
sw.Start();
int t1 = CountCharacters(1, "http://www.sina.com");
int t2 = CountCharacters(2, "http://www.baidu.com");
CounterToALargeNumber(1, LargeNumber);
CounterToALargeNumber(2, LargeNumber);
CounterToALargeNumber(3, LargeNumber);
CounterToALargeNumber(4, LargeNumber);
Console.WriteLine("Chars in http://www.sina.com :{0}", t1);
Console.WriteLine("Chars in http://www.baidu.com :{0}", t2);
}
private int CountCharacters(int id, string uristring)
{
WebClient wc1 = new WebClient();
Console.WriteLine("Starting call {0} : {1} ms", id, sw.Elapsed.TotalMilliseconds);
string result = wc1.DownloadString(new Uri(uristring));
Console.WriteLine(" Call {0} completed: {1} ms", id, sw.Elapsed.TotalMilliseconds);
return result.Length;
}
private void CounterToALargeNumber(int id, int value)
{
for(long i = 0; i < value; ++i)
;
Console.WriteLine(" End counting{0}: {1} ms", id, sw.Elapsed.TotalMilliseconds);
}
}
class MyDownloadStringHigh
{
Stopwatch sw = new Stopwatch();
public void DoRun()
{
const int LargeNumber = 6000000;
sw.Start();
Task<int> t1 = CountCharacters(1, "http://www.sina.com");
Task<int> t2 = CountCharacters(2, "http://www.baidu.com");
CounterToALargeNumber(1, LargeNumber);
CounterToALargeNumber(2, LargeNumber);
CounterToALargeNumber(3, LargeNumber);
CounterToALargeNumber(4, LargeNumber);
Console.WriteLine("Chars in http://www.sina.com :{0}", t1.Result);
Console.WriteLine("Chars in http://www.baidu.com :{0}", t2.Result);
}
private async Task<int> CountCharacters(int id, string uristring)
{
WebClient wc1 = new WebClient();
Console.WriteLine("Starting call {0} : {1} ms", id, sw.Elapsed.TotalMilliseconds);
string result = await wc1.DownloadStringTaskAsync(new Uri(uristring));
Console.WriteLine(" Call {0} completed: {1} ms", id, sw.Elapsed.TotalMilliseconds);
return result.Length;
}
private void CounterToALargeNumber(int id, int value)
{
for(long i = 0; i < value; ++i)
;
Console.WriteLine(" End counting{0}: {1} ms", id, sw.Elapsed.TotalMilliseconds);
}
}
static class DoAsyncStuff1
{
public static async Task<int> CalculateSumAsync(int i1, int i2)
{
int sum = await Task.Run(() => GetSum(i1, i2));
return sum;
}
private static int GetSum(int i1, int i2){return i1 + i2;}
}
static class DoStuff
{
public static async Task<int> FindSeriesSum(int i1)
{
int sum = 0;
for(int i=0; i < i1; ++i)
{
sum += i;
if(i%1000 == 0)
await Task.Yield();
}
return sum;
}
}
class Program
{
static void MainTest()
{
Console.WriteLine("2018-4-21");
Console.WriteLine("**********************************");
//MyDownloadStringLow ds1 = new MyDownloadStringLow();
//MyDownloadStringHigh ds2 = new MyDownloadStringHigh();
//ds1.DoRun();
//ds2.DoRun();
Console.WriteLine("************************************************");
Task<int> value = DoStuff.FindSeriesSum(1000000);
CountBig(100000);
CountBig(100000);
CountBig(100000);
CountBig(100000);
Console.WriteLine("Sum:{0}", value.Result);
}
private static void CountBig(int p)
{
for(int i = 0; i < p ; ++i)
;
}
}
}