c# - Is the string ctor the fastest way to convert an IEnumerable to string -
c# - Is the string ctor the fastest way to convert an IEnumerable<char> to string -
i've edited question incorporate valid points raised in comments.
i musing on my reply previous question , started wonder, this,
return new string(charsequence.toarray()); the best way convert ienumerable<char> string. did little search , found question asked here. reply asserts that,
string.concat(charsequence) is improve choice. next reply question, stringbuilder enumeration approach suggested,
var sb = new stringbuilder(); foreach (var c in chars) { sb.append(c); } homecoming sb.tostring(); while may little unwieldy include completeness. decided should little test, code used @ bottom.
when built in release mode, optimizations, , run command line without debugger attached results this.
1000000 iterations of "concat" took 1597ms.
1000000 iterations of "new string" took 869ms.
1000000 iterations of "sb" took 748ms.
to reckoning, new string(...toarray()) close twice fast string.concat method. stringbuilder marginally faster still but, awkward utilize extension.
should stick new string(...toarray()) or, there i'm missing?
using system; using system.collections.generic; using system.diagnostics; using system.linq; using system.text; class programme { private static void main() { const int iterations = 1000000; const string testdata = "some reasonably little test data"; testfunc( chars => new string(chars.toarray()), trueenumerable(testdata), 10, "new string"); testfunc( string.concat, trueenumerable(testdata), 10, "concat"); testfunc( chars => { var sb = new stringbuilder(); foreach (ver c in chars) { sb.append(c); } homecoming sb.tostring(); }, trueenumerable(testdata), 10, "sb"); console.writeline("----------------------------------------"); testfunc( string.concat, trueenumerable(testdata), iterations, "concat"); testfunc( chars => new string(chars.toarray()), trueenumerable(testdata), iterations, "new string"); testfunc( chars => { var sb = new stringbuilder(); foreach (ver c in chars) { sb.append(c); } homecoming sb.tostring(); }, trueenumerable(testdata), iterations, "sb"); console.readkey(); } private static tresult testfunc<tdata, tresult>( func<tdata, tresult> func, tdata testdata, int iterations) { var dummyresult = default(tresult); var stopwatch = stopwatch.startnew(); (var = 0; < iterations; i++) { dummyresult = func(testdata); } stopwatch.stop(); console.writeline( "{0} iterations of \"{1}\" took {2}ms.", iterations, func.method, stopwatch.elapsedmilliseconds); } } private static ienumerable<t> trueenumerable<t>(ienumerable<t> sequence) { foreach (var t in sequence) { yield homecoming t; } }
it's worth noting these results, whilst true case of ienumerable purists point of view, not thus. illustration if have char array if passed ienumerable faster phone call string constructor.
the results:
sending string ienumerable<char> 10000 iterations of "new string" took 157ms. 10000 iterations of "sb inline" took 150ms. 10000 iterations of "string.concat" took 237ms. ======================================== sending char[] ienumerable<char> 10000 iterations of "new string" took 10ms. 10000 iterations of "sb inline" took 168ms. 10000 iterations of "string.concat" took 273ms. the code:
static void main(string[] args) { testcreation(10000, 1000); console.readline(); } private static void testcreation(int iterations, int length) { char[] chars = getchars(length).toarray(); string str = new string(chars); console.writeline("sending string ienumerable<char>"); testcreatemethod(str, iterations); console.writeline("==========================================================="); console.writeline("sending char[] ienumerable<char>"); testcreatemethod(chars, iterations); console.readkey(); } private static void testcreatemethod(ienumerable<char> testdata, int iterations) { testfunc(chars => new string(chars.toarray()), testdata, iterations, "new string"); testfunc(chars => { var sb = new stringbuilder(); foreach (var c in chars) { sb.append(c); } homecoming sb.tostring(); }, testdata, iterations, "sb inline"); testfunc(string.concat, testdata, iterations, "string.concat"); } c# performance
Comments
Post a Comment