结论,
1,在数量极小时,stringbuilder并不比s+= 效率高。
2,在数量较大时,stringbuilder 显然比s+=效率高。
3,函数传值 byval和传参考by ref通常情况(所传递的对象不大)下,效率几无区别。
VB代码如下,
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim col As New NameValueCollection
col.Add("boat_165", "威士特丹号ms Westerdam")
col.Add("boat_180", "海洋帝王号Majesty of the Seas")
col.Add("boat_190", "诗歌号MSC Poesia")
col.Add("boat_195", "海洋航行者号Voyager of the Seas")
col.Add("boat_196", "海洋领航者号Navigator of the Seas")
col.Add("boat_197", "海洋水手号Mariner of the Seas")
col.Add("boat_198", "海洋探险者号Explorer of the Seas")
col.Add("boat_199", "海洋自由号Freedom of the Seas")
col.Add("boat_200", "海洋富丽号Grandeur of the seas")
col.Add("boat_201", "海洋幻丽号Enchantment of the Seas")
col.Add("boat_202", "海洋梦幻号Vision of the Seas")
col.Add("boat_203", "海洋荣光号Splendour of the Seas")
col.Add("boat_205", "海洋迎风号Rhapsody of the Seas")
col.Add("boat_206", "尖峰号Celebrity Summit")
col.Add("boat_207", "千禧号Celebrity Millennium")
col.Add("boat_208", "无极号Celebrity Infinity")
col.Add("boat_209", "星座号Celebrity Constellation")
col.Add("boat_210", "世纪号Celebrity Century")
col.Add("boat_211", "海洋冒险者号Adventure of the Seas")
col.Add("boat_212", "银星号Mercury")
col.Add("boat_213", "阿姆斯特丹号ms Amsterdam")
col.Add("boat_214", "诺丹号ms Noordam")
col.Add("boat_215", "如德丹号ms Zuiderdam")
col.Add("boat_216", "欧仕特丹号ms Oosterdam")
col.Add("boat_275", "海洋神话号Legend of the seas")
col.Add("boat_350", "经典号Costa Classica")
col.Add("boat_353", "海洋绿洲号Oasis of the seas")
col.Add("boat_354", "新浪漫号Costa neoRomantica")
col.Add("boat_357", "欧偌丹号ms Eurodam ")
col.Add("boat_401", "爱兰歌娜号Costa Allegra")
col.Add("boat_402", "处女星号SuperStar Virgo")
col.Add("boat_413", "幻想曲号MSC Fantasia")
col.Add("boat_425", "辉煌号MSC Splendida")
col.Add("boat_429", "挪威爱彼号Norwegian Epic")
col.Add("boat_444", "挪威翡翠号Norwegian Jade")
col.Add("boat_453", "奥德赛号Seabourn Odyssey ")
col.Add("boat_463", "星辰公主号Star Princess")
col.Add("boat_464", "天枰星号SuperStar Libra")
col.Add("boat_466", "双鱼星号Star Pisces")
col.Add("boat_467", "命运女神号Costa Magica")
col.Add("boat_468", "挪威宝石号Norwegian Jewel")
col.Add("boat_469", "炫目号Costa Luminosa")
col.Add("boat_470", "唯美号Costa Deliziosa")
col.Add("boat_471", "华伦丹号ms Volendam")
col.Add("boat_472", "皇冠公主号Crown Princess")
col.Add("boat_473", "海洋魅丽号Allure of the Seas")
col.Add("boat_476", "华丽号MSC Magnifica")
col.Add("boat_477", "音乐号MSC Musica")
col.Add("boat_478", "美国之傲号Pride of America")
col.Add("boat_479", "维丹号ms Veendam")
col.Add("boat_480", "新阿姆斯特丹号ms Nieuw Amsterdam")
col.Add("boat_481", "海洋光辉号Brilliance of the Seas")
col.Add("boat_482", "挪威珠宝号Norwegian Gem")
col.Add("boat_483", "红宝石公主号Ruby Princess")
col.Add("boat_484", "黄金公主号Golden Princess")
col.Add("boat_485", "至尊公主号Grand Princess")
col.Add("boat_486", "翡翠公主号Emerald Princess")
col.Add("boat_487", "加勒比公主号Caribbean Princess")
col.Add("boat_488", "珊瑚公主号Coral Princess")
col.Add("boat_489", "海岛公主号Island Princess")
col.Add("boat_490", "钻石公主号Diamond Princess ")
col.Add("boat_491", "皇家公主号Royal Princess")
col.Add("boat_492", "蓝宝石公主号Sapphire Princess")
col.Add("boat_493", "太阳公主号Sun Princess")
col.Add("boat_494", "黎明公主号Dawn Princess")
col.Add("boat_495", "碧海公主号Sea Princess")
col.Add("boat_496", "大溪地公主号Tahitian Princess")
col.Add("boat_497", "太平洋公主号Pacific Princess")
col.Add("boat_498", "歌剧号MSC Opera")
col.Add("boat_499", "抒情号MSC Lirica")
col.Add("boat_500", "序曲号MSC Sinfonia")
col.Add("boat_501", "和睦号MSC Armonia")
col.Add("boat_502", "管乐号MSC Orchestra")
col.Add("boat_503", "挪威明珠号Norwegian Pearl")
col.Add("boat_505", "海洋自主号Liberty of the Seas ")
col.Add("boat_507", "奇观号Disney Wonder")
col.Add("boat_508", "海皇号Monarch of the Seas")
col.Add("boat_509", "挪威天空号Norwegian Sky")
col.Add("boat_510", "海洋独立号Independence of the Seas ")
col.Add("boat_511", "海洋灿烂号Radiance of the Seas")
col.Add("boat_512", "挪威精神号Norwegian Spirit")
col.Add("boat_513", "挪威之晨号Norwegian Dawn")
col.Add("boat_514", "宝瓶星号Star Aquarius")
col.Add("boat_515", "水晶宁静号Crystal Serenity")
col.Add("boat_516", "辉宏号Costa Favolosa")
col.Add("boat_517", "海洋公主号Ocean Princess")
col.Add("boat_518", "维多利亚号Costa Victoria")
col.Add("boat_520", "神曲号MSC Divina")
col.Add("boat_521", "协和号Costa Concordia")
col.Add("boat_522", "银云号Silver Cloud")
col.Add("boat_523", "银风号Silver Wind")
col.Add("boat_524", "银影号Silver Shadow")
col.Add("boat_525", "银啸号Silver Whisper")
col.Add("boat_526", "银神号Silver Spirit ")
col.Add("boat_527", "银海探索号Silver Explorer")
col.Add("boat_528", "交响乐号Crystal Symphony")
col.Add("boat_529", "远征号Celebrity Xpedition")
col.Add("boat_530", "极致号Celebrity Solstice")
col.Add("boat_531", "季候号Celebrity Equinox")
col.Add("boat_532", "新月号Celebrity Eclipse")
col.Add("boat_533", "嘉印号Celebrity Silhouette")
col.Add("boat_534", "梦想号Disney Dream")
col.Add("boat_535", "前进号FRAM")
col.Add("boat_536", "50年胜利号")
col.Add("boat_537", "海洋玫瑰号OCEAN ROSE")
col.Add("boat_538", "大西洋号Atlantica Atlantica")
col.Add("boat_539", "传奇号Seabourn Legend")
col.Add("boat_540", "精灵号Seabourn Spirit")
col.Add("boat_541", "旅行者号Seabourn Sojourn")
col.Add("boat_542", "探索号Seabourn Quest")
col.Add("boat_543", "自豪号Seabourn Pride")
col.Add("boat_545", "创意号Carnival Imagination")
col.Add("boat_546", "地中海号Costa Mediterranea")
col.Add("boat_547", "光辉号Carnival Splendor")
col.Add("boat_548", "光荣号Carnival Glory")
col.Add("boat_551", "海洋旋律号Serenade of the Seas")
col.Add("boat_552", "海洋珠宝号Jewel of the Seas")
col.Add("boat_553", "欢欣号Carnival Elation")
col.Add("boat_554", "幻想号Disney Fantasy")
col.Add("boat_555", "佳名号Carnival Sensation")
col.Add("boat_557", "凯旋号Carnival Triumph")
col.Add("boat_558", "乐园号Carnival Paradise")
col.Add("boat_559", "雷丹号ms Ryndam")
col.Add("boat_560", "灵感号Carnival Inspiration")
col.Add("boat_561", "鹿特丹号ms Rotterdam")
col.Add("boat_562", "马士丹号ms Maasdam")
col.Add("boat_563", "美恋号Costa Fascinosa")
col.Add("boat_564", "梦幻号Carnival Fantasy")
col.Add("boat_565", "魔力号Disney Magic")
col.Add("boat_566", "魔力号Carnival Magic")
col.Add("boat_570", "挪威飞鸟号Norwegian Breakaway")
col.Add("boat_574", "挪威太阳号Norwegian Sun")
col.Add("boat_577", "挪威之星号Norwegian Star")
col.Add("boat_580", "普林盛丹号ms Prinsendam")
col.Add("boat_582", "清风号Carnival Breeze")
col.Add("boat_583", "塞琳娜号Costa Serena")
col.Add("boat_584", "尚丹号ms Zaandam")
col.Add("boat_585", "神往号Carnival Ecstasy")
col.Add("boat_586", "神逸号Carnival Fascination")
col.Add("boat_587", "胜利号Carnival Victory")
col.Add("boat_588", "史特丹号ms Statendam")
col.Add("boat_589", "太平洋号Costa Pacifica")
col.Add("boat_592", "幸运号Costa Fortuna")
col.Add("boat_593", "旋律号MSC Melody")
col.Add("boat_594", "英勇号Carnival Valor")
col.Add("boat_595", "征服号Carnival Conquest")
col.Add("boat_596", "自豪号Carnival Pride")
col.Add("boat_598", "自由号Carnival Freedom")
col.Add("boat_599", "自主号Carnival Liberty")
col.Add("boat_600", "传奇号Carnival Legend")
col.Add("boat_601", "佳运号Carnival Destiny")
col.Add("boat_602", "奇迹号Carnival Miracle")
col.Add("boat_603", "双子星号")
col.Add("boat_604", "珍爱号MSC Preziosa")
Dim date1 As DateTime
Dim s As String
date1 = Now
For i = 0 To 10000
s = PrintKeysAndValues1(col)
Next
Response.Write("<hr>it take " + (Now - date1).Duration.Ticks.ToString + " to call by value by stringbuilder </hr>")
date1 = Now
For i = 0 To 10000
s = PrintKeysAndValues2(col)
Next
Response.Write("<hr>it take " + (Now - date1).Duration.Ticks.ToString + " to call by value by string join </hr>")
date1 = Now
For i = 0 To 10000
s = PrintKeysAndValues3(col)
Next
Response.Write("<hr>it take " + (Now - date1).Duration.Ticks.ToString + " to call by ref by stringbuilder </hr>")
date1 = Now
For i = 0 To 10000
s = PrintKeysAndValues4(col)
Next
Response.Write("<hr>it take " + (Now - date1).Duration.Ticks.ToString + " to callby ref by string join </hr>")
End Sub
Public Shared Function PrintKeysAndValues1(ByVal myCol As NameValueCollection) As String
Dim sbd As New StringBuilder
For Each s In myCol.AllKeys
sbd.Append(s + ":" + myCol(s) + ";")
Next s
Return sbd.ToString
End Function 'PrintKeysAndValues
Public Shared Function PrintKeysAndValues2(ByVal myCol As NameValueCollection) As String
Dim sbd As String = ""
For Each s In myCol.AllKeys
sbd += (s + ":" + myCol(s) + ";")
Next s
Return sbd.ToString
End Function 'PrintKeysAndValues
Public Shared Function PrintKeysAndValues3(ByRef myCol As NameValueCollection) As String
Dim sbd As New StringBuilder
For Each s In myCol.AllKeys
sbd.Append(s + ":" + myCol(s) + ";")
Next s
Return sbd.ToString
End Function 'PrintKeysAndValues
Public Shared Function PrintKeysAndValues4(ByRef myCol As NameValueCollection) As String
Dim sbd As String = ""
For Each s In myCol.AllKeys
sbd += (s + ":" + myCol(s) + ";")
Next s
Return sbd.ToString
End Function 'PrintKeysAndValues
输 出结果如下:
it take 20331163 to call by value by string join
it take 6490371 to call by ref by stringbuilder
it take 20631180 to call by ref by string join