本文介绍了返回一个字符串数组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.
推荐答案
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]
这篇关于返回一个字符串数组VS Out参数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!