作者willieliao (Willie Liao)
看板C_Sharp
標題Re: [問題] 程式跑很慢
時間Wed Sep 27 05:28:49 2006
In C#, C++ and Java strings are all immutable. Which means
that when you use +=, you are actually creating a new string object
with concateated strings and then assign your variable to the new
string object.
Please consider use the stringbuilder class. Your programme would be
much faster that way.
※ 引述《Arton0306 (沒有怎麼辦)》之銘言:
: http://oz.nthu.edu.tw/~u922571/aa.txt
: 上面這一段程式碼跑得非常慢
: 程式碼大家隨便看一下就好
: 我解釋一下
: 主要是有個跑15000次的大迴圈(最外層那個)
: 其它小迴圈最多不超過2次
: 程式中有個min變數
: min越小 有一個字串變數outputError 就會存越多字
: 當min=0時
: outputError 會存到15000行左右的字
: 大迴圈跑幾次 outputError 就有幾行
: 請問改變字串有那麼慢嗎
: 難道是每次改變就要重分記憶體 所以很慢??
: 可是我就是要一直改變字串呀(一直加字)
: 請問有比較好的方法嗎??
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 208.238.98.1
推 Arton0306:非常感謝 原來C#有這樣的類別 :D 09/27 05:57
推 Arton0306:果然快非常非常多^^ 09/27 06:27
推 cplusplus:這東西蠻基本也很多人問 應該放到精華區 10/01 16:41