string s = "";
long t1 = system.datetime.now.ticks;
for(int i=0;i<10000;i++)s += convert.tostring(i);
long t2 = system.datetime.now.ticks;
long t3 = system.datetime.now.ticks;
system.text.stringbuilder sb = new system.text.stringbuilder();
for(int i=0;i<10000;i++)sb.append(convert.tostring(i));
string s1 = sb.tostring();
long t4 = system.datetime.now.ticks;
long t5 = system.datetime.now.ticks;
system.text.stringbuilder sb1 = new system.text.stringbuilder(5000);
for(int i=0;i<10000;i++)sb1.append(i);
string s2 = sb1.tostring();
long t6 = system.datetime.now.ticks;
long t7 = system.datetime.now.ticks;
system.text.stringbuilder sb2 = new system.text.stringbuilder(10000);
for(int i=0;i<10000;i++)sb2.append(i);
string s3 = sb2.tostring();
long t8 = system.datetime.now.ticks;
listviewitem item1 = result.items.add("使用string类,利用operator +进行操作");
item1.subitems.add(convert.tostring((t2-t1)/10000));
listviewitem item2 = result.items.add("使用stringbuilder类,不对初始大小赋值");
item2.subitems.add(convert.tostring((t4-t3)/10000));
listviewitem item3 = result.items.add("使用stringbuilder类,对初始大小赋值为申请数量的1/2");
item3.subitems.add(convert.tostring((t6-t5)/10000));
listviewitem item4 = result.items.add("使用stringbuilder类,对初始大小赋值为申请数量");
item4.subitems.add(convert.tostring((t8-t7)/10000));
2、异常对性能的影响,当然,下面这个例子没有进行异常的分发、捕获等,所以时间不长。
int j = 0;
long t1 = system.datetime.now.ticks;
for(int i = 0; i < 10000; i++)
{
try
{
j = i;
throw new system.exception();
}
catch {}
}
long t2 = system.datetime.now.ticks;
long t3 = system.datetime.now.ticks;
for(int i = 0; i < 10000; i++)
{
try
{
j = i;
//throw new system.exception();
}
catch {}
}
long t4 = system.datetime.now.ticks;
result.items.clear();
listviewitem item1 = result.items.add("抛出10000个异常");
item1.subitems.add(convert.tostring((t2-t1)/10000));
listviewitem item2 = result.items.add("屏蔽异常");
item2.subitems.add(convert.tostring((t4-t3)/10000));
3、使用foreach循环与使用for循环(差别较大)
string s = "monkeys!";
int dummy = 0;
system.text.stringbuilder sb = new system.text.stringbuilder(s);
for(int i = 0; i < 1000000; i++)sb.append(s);
s = sb.tostring();
long t1 = system.datetime.now.ticks;
foreach (char c in s) dummy++;
long t2 = system.datetime.now.ticks;
dummy = 0;
long t3 = system.datetime.now.ticks;
for (int i = 0; i < 1000000; i++)dummy++;
long t4 = system.datetime.now.ticks;
result.items.clear();
listviewitem item1 = result.items.add("使用foreach循环");
item1.subitems.add(convert.tostring((t2-t1)/10000));
listviewitem item2 = result.items.add("使用for循环");
item2.subitems.add(convert.tostring((t4-t3)/10000));
4、latebinding和directcall的比较(差别也比较大)
long t1 = system.datetime.now.ticks;
latebindingtest lbt = new latebindingtest();
for(int i=0;i<100000;i++)
{
string s = lbt.getsomewhat();
}
long t2 = system.datetime.now.ticks;
long t3 = system.datetime.now.ticks;
type t = type.gettype("perfdemo.latebindingtest");
methodinfo mi = t.getmethod("getsomewhat");
for(int i=0;i<100000;i++)
{
object s2 = mi.invoke(lbt,null);
}
long t4 = system.datetime.now.ticks;
result.items.clear();
listviewitem item1 = result.items.add("直接调用class的方法(微秒)");
item1.subitems.add(convert.tostring((t2-t1)/10));
listviewitem item2 = result.items.add("采用迟绑定的方式class的方法(微秒)");
item2.subitems.add(convert.tostring((t4-t3)/10));
5、这是上面例子中使用的class定义:
public class latebindingtest
{
public string getsomewhat()
{
return "hello,fqq!";
}
}
6、大家可以结合taskmgr和performancemonitor来进行监视,结论还是比较有意思的
新闻热点
疑难解答
图片精选