c# - Efficiency for Path.GetFileName storing in a string or reusing method call -
I'm very curious to know what would be the best practice / efficient method. I have a way where I have an OpenFileDialog where I have paths in 5 places inside the method. GetFileName (openFileDialog.FileName) to be used. Which is the best way: either I call GetFileName or save it in string variable and then use that string variable?
"+ + Path.GetFileName (openFileDialog.FileName) +" "string fileName = path.get filename (openFileDialog.FileName);" + + filename + ""
Other responses already mentioned before considering the readability and maintenance, in the performance of this small contract string storage option (slightly ) Will display profit
void Main () {string p = @ "d: \ temp \ file.txt"; string result; stopwatch SW = new stopwatch (); sw.Start () ; (Int i = 0; I & lt; 100000; i ++) {Results = "" + PathgateFileName (P) + "";} Sw.Stop (); console.witline ("PathgateFilename:" + sw.Elapsed.ToString ()); Sw = new stopwatch (); sw.Start (); string file = path.getFileName (P) ; (Int i = 0; I & lt; 100000; i ++) {Results = "+ + +" file; "";} Sw.Stop (); Console.lightline ("String Conceit:" + sw. Elapsed.ToString ());} Result:
Pathgate filename: 205632 String concatenated: 65234 Of course , This is microprotection and should not be given much attention.
In this case the readability of the code is more important.
Comments
Post a Comment