performance while doing string concatenation - algorithm string strings c# -
i using following code append strings string res = string.empty; int ite = 100000; for(int i= 0; < ite; i++) { res += "5"; } it taking lot of time, later changed code string res = string.empty; int ite = 100000; res = getstr(ite / 2) + getstr(ite - (ite / 2)); //body of getstr method private static string getstr(int p) { if (p == 1) return "5"; else if (p == 0) return string.empty; string r1 = getstr(p / 2); //recursive string r2 = getstr(p - (p / 2)); //recursive return (r1 + r2); } which in opinion nothing number of times strings concatenated approximately same in previous approach. but using approach there significant improvement in performance code taking around 2500 ms (on machine) taking 10 ms. i ran profiler on cpu time , couldn't arrive @ understanding why there improvement in performance. can please explain this. note: intentionally not using stringbuilder, in order understand above. ...