歡迎辭歡迎來到“篤志以礪,決起而飛”! 如果您是第一次來到本站,建議訪問 本站導讀以便更快地了解本站。 如果您喜歡本站, 歡迎訂閱。 | 最近大家都在討論C#的執行效率問題。由於採用的測試方法不同,測試結果差異很大。有些人得到結論,C#的速度竟然比 C++ 慢 20 倍!但是這樣的測試是沒有準確性的。 為什麼這麼說呢?語言的執行效率是要考慮編譯到機器碼後用戶代碼執行的緊湊性和高效性,而不是代碼本身的執行速度。因為代碼可以優化,.NET framework 也在升級,但編譯的機器碼卻是死的。編譯機制才是影響執行效率的最重要的原因。其它原因都不是根本原因 單從計算和執行的角度來說,C#應該還是比較快的。 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| using System;
namespace test
{
public class test
{
static void Main()
{
long j = 0;
int t = Environment.TickCount;
for (int i = 0; i < = 10000000; i++)
{
j += i;
}
Console.WriteLine("Time:{0},Return:{1}.", Environment.TickCount - t, j);
Console.ReadLine();
}
}
} |
結果用時:140 RETURN: 50000005000000 這是一個送給初學 c#的程序,簡單明了,用於計算平均數,請自己研究一下它的結構。 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
| using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Test
{
class Program
{
static void Main(string[] args)
{
do
{
Console.ResetColor();
Console.WriteLine("rn歡迎使用本程序。請輸入一系列數。rn");
//開始記錄數字
int s = 0;
double z = 0;
do
{
Console.Write("數字(按回車確認,直接按回車結束):_");
string i = Console.ReadLine();
int n;
if (int.TryParse(i, out n))
{
s++;
z += n;
}
else
{
break;
}
}
while (true);
if (s == 0)
{
Console.WriteLine("-> 參數錯誤。");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("-> 平均數:{0}。感謝您的使用。", (double)(z / s));
}
}
while (true);
}
}
} |
使用 C# 實現 Bin 類,快速方便地管理 byte[] 類型一直是我的心愿。因為在 C C++ C# VB 等語言中,byte[] 類型是常用的,但卻沒有太多方便的命令去處理它。因此我編寫了 Bin 類。這個類實現了方便的管理,封裝了大部分 Byte[] 操作,如添加、刪除、查找等。在編寫中注重速度、效率和方便性,支持索引器[index]直接引用,操作符重載等。 同時這也是我寫的“ToEasy”C#簡單化代碼的一部分,namespace 就是 ToEasy 了! 下面就是我寫的代碼: 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
/* Bin 操作類
* 版本:1.0
* --
* 歡迎訪問:http://ceeji.net (博客)
*
*/
namespace ToEasy
{
/// <summary>
/// 相當於易語言中的“字節集”數據類型,可以容納字節數組數據。簡化了對數據的操作。
/// </summary>
public class Bin
{
//字段
byte[] data = new byte[0];
//屬性
public Byte[] ByteArray
{
get
{
return this.data;
}
}
public int Length
{
get
{
return this.data.Length;
}
}
//隱式轉換
public static implicit operator Bin(byte[] from)
{
return new Bin(from);
}
public static implicit operator byte[] (Bin from)
{
return (byte[])from.data.Clone();
}
//運算
public static bool operator ==(Bin bin, byte[] o)
{
return bin.ByteArray == o;
}
public static bool operator !=(Bin bin, byte[] o)
{
return bin.ByteArray != o;
}
public static bool operator ==(byte[] o, Bin bin)
{
return bin == o;
}
public static bool operator !=(byte[] o, Bin bin)
{
return bin != o;
}
public static Bin operator +(Bin bin1, Bin bin2)
{
return bin1.GetAppend((byte[])bin2);
}
//索引器
public byte this[int index]
{
get
{
return this.GetByte(index);
}
set
{
this.data[index] = value;
}
}
//構造
public Bin()
{
}
public Bin(byte[] from)
{
this.FromByteArray(from);
}
public Bin(int length)
{
this.data = new byte[length];
}
//方法
public void Clear()
{
data = new byte[0];
}
public void FromByteArray(byte[] byteArray)
{
data = (byte[])byteArray.Clone();
}
public void FromFile(string fileName)
{
this.FromByteArray(File.ReadAllBytes(fileName));
}
/// <summary>
/// 向現在的字節集的末尾添加數據。
/// </summary>
/// <param name="append">字節集數據,可以為 byte[] 或 Bin.</param>
public void Append(byte[] append)
{
byte[] nbyte = new byte[this.Length + append.Length];
this.data.CopyTo(nbyte, 0);
append.CopyTo(nbyte, this.Length);
this.FromByteArray(nbyte);
}
public Bin GetAppend(byte[] append)
{
Bin bin = new Bin();
byte[] nbyte = new byte[this.Length + append.Length];
this.data.CopyTo(nbyte, 0);
append.CopyTo(nbyte, this.Length);
bin.FromByteArray(nbyte);
return bin;
}
public byte GetByte(int index)
{
return data[index];
}
public byte[] GetLeft(int length)
{
byte[] ret = new byte[length];
Array.Copy(data, 0, ret, 0, length);
return ret;
}
public byte[] GetData(int startIndex, int length)
{
byte[] ret = new byte[length];
Array.Copy(data, startIndex, ret, 0, length);
return ret;
}
public byte[] GetRight(int length)
{
byte[] ret = new byte[length];
Array.Copy(data, this.Length - length - 1, ret, 0, length);
return ret;
}
/// <summary>
/// 返回當前對象的字符串形式,例如 { 57, 46 }
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (data.Length == 0)
{
return "{ }";
}
StringBuilder str = new StringBuilder("{ ");
foreach (byte each in data)
{
str.Append(each.ToString());
str.Append(", ");
}
str.Remove(str.Length - 2, 2);
str.Append(" }");
return str.ToString();
}
/// <summary>
/// 移除某項。這會很浪費時間,盡量不要這樣做。
/// </summary>
/// <param name="startindex"></param>
/// <param name="length"></param>
public void Remove(int startindex, int length)
{
if (length < 1)
{
return;
}
byte[] ndata = new byte[this.Length - length];
int j = 0;
for (int i = 0; i <= this.Length - 1; i++)
{
if ((i >= startindex & i < = startindex + length) == false)
{
ndata[j] = this[i];
j++;
}
}
this.data = (byte[])ndata.Clone();
}
/// <summary>
/// 在當前字節集中查找子字節集。如果找到則返回索引位置,否則返回 -1。
///
/// <param name="findbin">要找的數據</param>
/// <returns>索引位置</returns>
public int Find(Bin findbin)
{
byte[] b = findbin.ByteArray ;
int l = findbin.Length;
int z = this.Length - l + 1;
for (int i = 0; i < = z; i++)
{
if (findbin.GetData(i, l) == b)
{
return i;
}
}
return -1;
}
/// <summary>
/// 在當前字節集中從後向前查找子字節集。如果找到則返回索引位置,否則返回 -1。
///
/// <param name="findbin">要找的數據</param>
/// <returns>索引位置</returns>
public int FindLast(Bin findbin)
{
byte[] b = findbin.ByteArray;
int l = findbin.Length;
int z = this.Length - l + 1;
for (int i = z; i == 0; i--)
{
if (findbin.GetData(i, l) == b)
{
return i;
}
}
return -1;
}
/// <summary>
/// 執行替換。
/// </summary>
/// <param name="startindex">開始位置索引</param>
/// <param name="length">替換長度</param>
/// <param name="bin">替換成的數據</param>
public bool Replace(int startindex, int length, Bin bin)
{
if (startindex > this.Length - 1)//開始位置超標
{
return false;
}
if (startindex + length > this.Length)//結束位置超標
{
return false;
}
if (bin.Length < length)//提供數據不足
{
return false;
}
byte[] b = bin.ByteArray;
int j = 0;
for (int i = startindex; i <= startindex + length - 1; i++)
{
this.data[i] = b[j];
j++;
}
return true;
}
/// <summary>
/// 將當前字節集寫到指定的文件中。如果文件已存在,該文件將被覆蓋。
///
/// <param name="filename">文件名</param>
/// <param name="append">是否追加</param>
public void WriteToFile(string filename, bool append)
{
if (append == false)
{
File.WriteAllBytes(filename, this.ByteArray);
}
else
{
FileStream stream = new FileStream (filename,FileMode.Append );
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(this.ByteArray);
writer.Close();
stream.Close();
}
}
/// <summary>
/// 將當前字節集寫入文件基礎流。
/// </summary>
/// <param name="stream">文件基礎流</param>
public void WriteToFileStream(FileStream stream)
{
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(this.ByteArray);
writer.Close();
}
}
} |
“到失去的時候才知道珍惜”這句話說的一點也不錯。我現在深深體會到了這一點。 上高中之前,我家離學校一直很近,回家也很方便,從未感覺到有什麼不普通的。可是現在我感受到了。 這是因為,高中我來到了附中。它離我家確實不近,我只好住校了。 “終於放學了!”我聽到了久盼的鈴聲,火速收拾書包。元旦了,可以回家了!我幾乎手舞足蹈地出了校園。 等着1路車,我的心早已飛回了家。一幅幅溫暖的畫面讓我恨不得施展魔法回家(雖然我知道這不可能)——和父母在客廳看電視;吃着家中的五穀雜糧;在我明亮的房間里學習;用我的電腦聽音樂,看新聞;躺在家中柔軟的床上……多麼普通的事情!可這足以使我激動一路。 衝到家門,狂插鑰匙,放眼望去——家依舊是原來的家,寧靜,溫暖,充滿生機。真的又一次和父母一起吃飯,看電視……晚上全家人出去散步,我呼吸着冬日裡清爽的冷氣,幾乎要陶醉—— 那天晚上,我竟然散了100分鐘的步。 …… 當我再次坐上了去學校的車,再次回到教室,再次寫數學卷時,我還沒有完全從三天的元旦假日中脫離出來。此刻,我越來越讚歎家的偉大,家中父母的偉大。再次遠離家,心中多了一股溫暖的力量。 2007.1.4周記 我站在一個奇怪的世界—— 這裡的人, 身上都非常骯髒。 但是他們是怎麼髒的, 我一直不知道。 人類崇尚乾淨整潔, 為什麼這個世界卻不能? 我心存疑惑。 那一天, 我斗膽問他們: 你們為什麼 不講究衛生 他們的回答讓我震驚—— 我們講究衛生? 呵呵! 我們是想講究, 但是,如果我們乾淨的話, 你就髒了! 我一頭霧水…… 原來做了一場夢。 也該起床了, 我說。 於是起床, 刷牙, 洗臉, 洗手, 喝水。 哦,是的,我就是用他們 保持自己的所謂“潔凈”的—— 偉大的水! 而我的夢, 純潔的夢, 就是水的呼喚! 七,十五,二零零六 | |
近期評論