返回一个字符串数组VS Out参数. [英] Returning an array of strings VS Out parameter.

查看:117
本文介绍了返回一个字符串数组VS Out参数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的函数中返回多个字符串,所以这在性能问题上会更好,即返回字符串数组或输出参数?

在此先谢谢您.

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.

推荐答案

如果您想了解一些关于性能比较的晦涩之处,最好的办法就是检查一下自己:这样您就可以得出实际的数字.一起工作.幸运的是,.NET提供了 Stopwatch类 [
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");
     }

运行3次会得到结果:

Out: 296
Ret: 269
Out: 265
Ret: 270
Out: 273
Ret: 279

那么在实践中呢?没关系!:笑:

[edit]:Doh:我忘记了两个例程-OriginalGriff [/edit]

So in practice? No difference!: laugh:

[edit]:Doh: I forgot the two routines - OriginalGriff[/edit]


我看不出它有什么大不同,但是您的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.


这篇关于返回一个字符串数组VS Out参数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆