c# - Quickest way to concatenate all String elements from List to String -


i have large amount of user objects on website, each containing list of forums administrate. have created page display paginated list of users administrate forum of forum names.

so example

travis - ftb, scr, swm, bsb

tom - ftb, scr, swm, bsb, trk, bsk

however users may administrate on 300 different topics , causing pages long time process.

i have tried following ways process faster, of them taking same amount of time (too long).

//join topicstring = string.join(", ", user.topics) (note topics.tostring returns 3 character id)  //stringbuilder stringbuilder stringbuilder = new stringbuilder(3*user.topics.count() + 2*user.topics.count()); foreach(var topic in user.topics) {    stringbuilder.append(topic.code);    stringbuilder.append(", "); } codes = stringbuilder.tostring(); codes = codes.remove(codes.length-2);   //classic concatenation foreach(var topic in user.topics) {     codes += topic.code;     codes += ", "; } codes = codes.remove(codes.length-2); 

each page contains 15 users

when 15 users contain around 3 topics each takes 1 second load page. page has 1 user containing on 100 topics, page load skyrockets near 20 seconds.

could there way threading speed things up?

as in comments mentioned performance issue caused fetching data database not string concatenation. , lazy loading confusing here.

string builder show power when string length more 1 million characters. when use string.join() method use string builder internally.

a bit performance improvement code set string builder length before converting string avoid creating of large string object twice:

// stringbuilder stringbuilder stringbuilder = new stringbuilder(); foreach (var topic in user.topics) {    stringbuilder.append(topic.code);    stringbuilder.append(", "); } stringbuilder.length--; codes = stringbuilder.tostring(); 

it not necessary specify string builder size because resizing algorithm fast enough.

i worked string builder class size more 100 million characters , in situation, approximately 10,000 times faster normal string concatenation.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -