c# - Passing method variables as parameters for code optimization -


i have method

protected list<string> wrapintwolines(string text, int linelimit)         {             ///there 2 lines first line can empty , whole data goes 2nd line             string[] originallines = text.split(new string[] { " " }, stringsplitoptions.none);              list<string> wrappedlines = new list<string>();              stringbuilder actualline = new stringbuilder();              int index=0;             while(actualline.length + originallines[index].length < linelimit)             {                 actualline.appendformat("{0} ",originallines[index]);                 index++;             }             wrappedlines.add(actualline.tostring());             actualline.clear();              while (index < originallines.length)             {                 actualline.appendformat("{0} ",originallines[index++]);             }             wrappedlines.add(actualline.tostring().trimend(' '));             return wrappedlines;         } 

being called inside loop

for(int i=0; i< items.count; i++) {     length += items[i].length + 2;     if (length > char_length)     {         var list = wrapintwolines(items[i], char_length - (length - items[i].length + 2));         subtitleslist.add(s.append(list[0]).tostring());          s = new stringbuilder().appendformat("{0}{1}",list[1],separator);         length = s.length;     }     else     {         s.appendformat("{0}{1}", items[i], separator);     } } 

my method creates 3 reference variable new on each iteration. working on optimization of code , planning implement method follows

protected list<string> wrapintwolines(string[] originallines, int linelimit, list<string> wrappedlines, stringbuilder actualline)             {                 ///there 2 lines first line can empty , whole data goes 2nd line                 //string[] originallines = text.split(new string[] { " " }, stringsplitoptions.none);                  //list<string> wrappedlines = new list<string>(); wrappedlines.clear();                  //stringbuilder actualline = new stringbuilder(); actualline.clear();     //rest remains same     } 

i think improve code not sure how improve or improve code or not. tools/techniques can use compare code optimization in terms of memory or speed? question is pattern pass method variables parameters(as in above method actualline etc)?

this change not improve performance significantly. garbage collectors java , c# optimized perform in collecting small short-lived objects wrappedlines , actualline. when clear wrappedlines instead of creating new one, gc still has collect strings contained in wrappedlines.

unless having performance problems, don't complicate code guessing @ performance optimizations. wrapintwolines method easier understand , less error-prone without parameters.

if having performance problems, first inside innermost loops - code gets executed often. appendformat requires run-time parsing of format string - perform worse append(" ").append(originallines[i]).

as far tools go, i've had best results running problem code multiple times , timing it. there more sophisticated tools available haven't found value in them. run multiple timing trials , average them single trial may skewed.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -