stringbuilder - Fast string insertion at front in c# -


i need insert strings @ beginning. right use stringbuilder.insert(0, stringtoinsert) insert @ front, it's taking lot of time (around 2 mins 80,000 strings).

the append() method runs lot faster (30 secs 80,000 strings), it's not order need. how can reverse order of strings (and not string itself) , decrease insertion time?

yes, reversing enumerable much faster.

for example:

var numstrings = 80000; var strings = new list<string>(); for(var = 0; < numstrings; i++) {     strings.add(guid.newguid().tostring()); }  var sw = new stopwatch(); sw.start(); var sb = new stringbuilder(); foreach(var str in enumerable.reverse(strings))     sb.append(str);  sw.stop(); sw.elapsedmilliseconds.dump(); // 13 milliseconds sb.dump();  sw = new stopwatch(); sw.start(); sb = new stringbuilder(); foreach(var str in strings)     sb.insert(0, str);  sw.stop(); sw.elapsedmilliseconds.dump(); // 42063 milliseconds sb.dump(); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -