通過unity profiler測試的**,及執行結果
using unityengine;
using unityengine.profiling;
using system.text;
#if unity_5_5_or_newer
using tprofiler = unityengine.profiling.profiler;
#else
using tprofiler = unityengine.profiler;
#endif
public
namespace test
teststring10000();
}// object new once
private stringbuilder _sb = new stringbuilder();
/// concat 10000 times
private
void
teststring10000()
tprofiler.endsample();
_sb.clear();
for(int i = 0; i < 10000; ++i)
string s2 = _sb.tostring();
tprofiler.endsample();
}/// concat 3 parts
private
void
teststring() _end", (int)time.time);
tprofiler.endsample();
// format tostring()
tprofiler.beginsample("string.format & int.tostring");
string s5 = string.format("txt_pre__end", ((int)time.time).tostring());
tprofiler.endsample();
_sb.clear();
string s6 = _sb.tostring();
tprofiler.endsample();
_sb.clear();
string s7 = _sb.tostring();
tprofiler.endsample();
_sb.clear();
string s8 = _sb.tostring();
tprofiler.endsample();
_sb.clear();
string s9 = _sb.tostring();
tprofiler.endsample();}}
}
執行結果:
可讀性一般
堆記憶體開銷低
執行效率較高
建議:
大量字串拼接時【必須使用】,記憶體及執行效率優化效果十分明顯
少量字串拼接時【酌情使用】,如呼叫頻度
3、+和concat
可讀性較好
堆記憶體開銷低
執行效率較高
在很多情況下,+會被優化為concat
建議:
大量字串拼接時【禁止使用】,記憶體及執行效率開銷非常大,無法忍受
少量字串拼接時【建議使用】
另外,對於string拼接時的int等基本資料型別,養成tostring()的習慣,避免box操作帶來的額外記憶體開銷。
C 中String與string的區別分析
一 區別分析 string 類,system.string string 型別,變數 兩者本質上yfsyvbtck沒有任何區別,都是system.string,string只是system.string的別名而已 唯一的區別在於如何按照 約定的來寫,如宣告變數時使用小寫string,使用system...
c 字串拼接效率
1 對於少量固定的字串拼接,如string s a b c 系統會優化成s string.concat a b c 不會新建多個字串。如果寫成string s a s b s c 則會建立三個新的字串。可見,它和stringbuilder有著相似的效率,比用 的拼接方式高效,並且 易於閱讀。stri...
string在c 中的各種用法分析
首先,為了在我們的程式中使用string型別,我們必須包含標頭檔案 如下 include 注意這裡不是string.h string.h是c字串標頭檔案 1 宣告乙個c 字串 宣告乙個字串變數很簡單 string str 這樣我們就宣告了乙個字串變數,但既然是乙個類,就有建構函式和析構函式。上面的宣...