Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to return multiple strings from my function so which is better regarding performance issue, i.e. Returning an array of string OR Out a parameter?

Thanks in advance.
Posted

If you want to know something obscure about performance comparisons, the best thing to do is to check for yourself: that way you get actual numbers to work with. Fortunately, .NET supplies the Stopwatch class[^] for just such events.

     string[] ar = File.ReadAllLines(@"F:\Temp\JustSomeText.txt");
     Stopwatch sOut = new Stopwatch();
     sOut.Start();
     for (int i = 0; i < 1000; i++)
         {
         stringsOut(out ar);
         }
     sOut.Stop();
     Stopwatch sRet = new Stopwatch();
     sRet.Start();
     for (int i = 0; i < 1000; i++)
         {
         ar = stringsRet();
         }
     sRet.Stop();
     Console.WriteLine("Out: {0}\nRet: {1}", sOut.ElapsedMilliseconds, sRet.ElapsedMilliseconds);

private void stringsOut(out string[] par)
     {
     par = File.ReadAllLines(@"F:\Temp\JustSomeText.txt");
     }
 private string[] stringsRet()
     {
     return File.ReadAllLines(@"F:\Temp\JustSomeText.txt");
     }
Running this three times gives the results:
Out: 296
Ret: 269
Out: 265
Ret: 270
Out: 273
Ret: 279
So in practice? No difference!: laugh:

[edit]:Doh: I forgot the two routines - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Jul-11 3:55am    
No wonder at all! My 5.
I don't know why Christian decided 'out' is more strict. Please see my comment.
--SA
OriginalGriff 5-Jul-11 4:03am    
I hadn't seen Christians answer when I posted mine: I would agree with you that the return type of "string[]" is strongly typed. Personally, I prefer a return value to an out parameter if there is only one such object to return - I think it reads more naturally.
Sergey Alexandrovich Kryukov 5-Jul-11 4:37am    
Nobody would argue against this natural thing. The problem if is 'out' better or is it acceptable or not. I think it is never better but quite acceptable especially as explicit 'out' is required at the call. See my comment to Christian's answer.
--SA
All Time Programming 5-Jul-11 4:27am    
My 5. Return value is always more preferred and work best in all cases than out. Then you may return a single string or an string[], return type will work more better than out.
I don't see how it could make much difference, but your out parameter is strongly typed, in an array, you need to know which is which. So, use the 'out'. And always ask yourself first, what makes my code more readable. Worry about performance when you notice issues, or when you're dealing with a lot of users, a lot of data, or both.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jul-11 3:54am    
Sorry, Christian, why do you think return is not strongly typed?

Interestingly, (very useful) FxCop does no pass 'out' parameters, but I think this is one of Microsoft's stupidities: parts of Microsoft contradict. 'Out' is fine because C# requires explicit specification of 'out' in call.
--SA
Christian Graus 5-Jul-11 3:56am    
It's an array of strings. What I mean is, a consumer of his methods, has no idea which string is which value. It's not defined which string in the array means what.
Sergey Alexandrovich Kryukov 13-Jul-11 3:18am    
I see your point. It does not mean lack of string typing. What you refer to are not types at all, it's just a huge abuse. I'm not sure OP means exactly that, but the point makes sense; some people think of strings as non-string data.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900