,NET STRING BUFFER使用 與時間計算
private void testString()
{
String tempstr = "abcdefghijklmnopqrstuvwxyz";
int times = 50000;
long aa, bb,cc,dd;
DateTime startDate,endDate;
DateTime startDate1, endDate1;
richTextBox1.AppendText("string測試開始\r\n");
startDate = DateTime.Now;
aa = startDate.Ticks;
String str = "";
for (int i = 0; i < times; i++)
{
str += tempstr;
}
endDate = DateTime.Now;
bb = endDate.Ticks;
richTextBox1.AppendText("執行時間︰" + ((Double)(bb - aa) / 10000) + "毫秒 = "+ (Double)(bb - aa) / 10000000+"秒\r\n");
richTextBox1.AppendText("StringBuilder測試開始\r\n");
startDate1 = DateTime.Now;
cc = startDate1.Ticks;
StringBuilder strb = new StringBuilder();
for (int i = 0; i < times; i++)
{
strb.Append(tempstr);
}
endDate1 = DateTime.Now;
dd = endDate1.Ticks;
richTextBox1.AppendText("執行時間︰" + (Double)(dd - cc) / 10000 + "毫秒 = " + (Double)(dd - cc) / 10000000+"秒");
richTextBox1.AppendText("效能差距︰" + (Double)(bb - aa) / (Double)(dd - cc)+ "倍");
}
引用來源
{
String tempstr = "abcdefghijklmnopqrstuvwxyz";
int times = 50000;
long aa, bb,cc,dd;
DateTime startDate,endDate;
DateTime startDate1, endDate1;
richTextBox1.AppendText("string測試開始\r\n");
startDate = DateTime.Now;
aa = startDate.Ticks;
String str = "";
for (int i = 0; i < times; i++)
{
str += tempstr;
}
endDate = DateTime.Now;
bb = endDate.Ticks;
richTextBox1.AppendText("執行時間︰" + ((Double)(bb - aa) / 10000) + "毫秒 = "+ (Double)(bb - aa) / 10000000+"秒\r\n");
richTextBox1.AppendText("StringBuilder測試開始\r\n");
startDate1 = DateTime.Now;
cc = startDate1.Ticks;
StringBuilder strb = new StringBuilder();
for (int i = 0; i < times; i++)
{
strb.Append(tempstr);
}
endDate1 = DateTime.Now;
dd = endDate1.Ticks;
richTextBox1.AppendText("執行時間︰" + (Double)(dd - cc) / 10000 + "毫秒 = " + (Double)(dd - cc) / 10000000+"秒");
richTextBox1.AppendText("效能差距︰" + (Double)(bb - aa) / (Double)(dd - cc)+ "倍");
}
引用來源
留言