在.NET 4中,System.IO.Path对该Combine方法具有以下重载:
public static string Combine(params string[] paths)
public static string Combine(string path1, string path2)
public static string Combine(string path1, string path2, string path3)
public static string Combine(string path1, string path2, string path3, string path4)
Run Code Online (Sandbox Code Playgroud)
第一个是在.NET 4中添加的,以支持任意数量的路径参数.第二个在早期版本中已经存在,所以我认为它是为了向后兼容而保留的.
但我很好奇其他重载的用途是什么.第一种方法签名是否已涵盖这些用例params?
编辑:我现在相信答案是"因为并非所有语言都params支持(并且传递没有params支持的数组是不方便的)".然而,stackoverflow蜂巢的想法似乎强烈反对.因此,作为妥协,我不接受任何答案.
Tim*_*oyd 28
我怀疑性能,因为你必须创建一个带有params的中间数组,加上遍历数组的开销等等.可能有一些内部等情况,其中有一个使用固定编号参数版本的好例子.
更新
我现在已经进行了性能测试来检查我的假设.这是我本来应该做的事情 - 我打破了自己的表演口号:
不要想 - 衡量.
我的假设并不完全正确,但并非完全错误.4固定参数版本比4 params版本略慢,但3和2固定变量表现明显更好.
对于当前接受的答案,性能测试工具存在许多问题,这些问题表明性能完全支持params版本 - 这是不正确的:
我有以下性能结果,其中包括2,3和4个参数变量,其中可以看出2和3变化的性能明显更好,而4变化的性能稍差.固定数量参数版本总体上更快,其中3个是这个问题最重要的(自.Net 1.1以来存在2个参数变体).
***2 Args***
params2:3018.44ms
params2:3007.61ms
params2:2988.52ms
params2:2992.33ms
params2:2995.89ms
args2 :1724.83ms
args2 :1723.97ms
args2 :1727.76ms
args2 :1720.42ms
args2 :1718.24ms
***3 Args***
params3:4168.37ms
params3:4169.61ms
params3:4165.63ms
params3:4161.51ms
params3:4153.61ms
args3 :3476.96ms
args3 :3483.40ms
args3 :3482.49ms
args3 :3595.15ms
args3 :3561.11ms
***4 Args***
params4:4992.71ms
params4:4985.51ms
params4:4995.63ms
params4:5002.47ms
params4:4993.99ms
args4 :4993.02ms
args4 :4992.93ms
args4 :4991.07ms
args4 :4993.04ms
args4 :4995.14ms
Run Code Online (Sandbox Code Playgroud)
测试:
public void MeasurePathPerformance()
{
const int TestIterations = 5;
const string Root = "C:\\xxxxxxxxxx";
string seg = new string('x', 10);
string path = null;
Action<string, Func<double>> test = (name, action) =>
{
for (int i = 0; i < TestIterations; i++)
{
Console.WriteLine("{0}:{1:F2}ms", name, action());
}
};
Console.WriteLine("***2 Args***");
Action p2 = () => path = Path.Combine(new[] { Root, seg });
test("params2", () => TimeTest(p2));
Action a2 = () => path = Path.Combine(Root, seg);
test("args2 ", () => TimeTest(a2));
Console.WriteLine("***3 Args***");
Action p3 = () => path = Path.Combine(new[] { Root, seg, seg });
test("params3", () => TimeTest(p3));
Action a3 = () => path = Path.Combine(Root, seg, seg);
test("args3 ", () => TimeTest(a3));
Console.WriteLine("***4 Args***");
Action p4 = () => path = Path.Combine(new[] { Root, seg, seg, seg });
test("params4", () => TimeTest(p4));
Action a4 = () => path = Path.Combine(Root, seg, seg, seg);
test("args4 ", () => TimeTest(a4));
Console.WriteLine(path);
}
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.GC.Collect")]
private static double TimeTest(Action action)
{
const int Iterations = 10 * 1000 * 1000;
Action gc = () =>
{
GC.Collect();
GC.WaitForFullGCComplete();
};
Action empty = () => { };
Stopwatch stopwatch1 = Stopwatch.StartNew();
for (int j = 0; j < Iterations; j++)
{
empty();
}
double loopElapsed = stopwatch1.Elapsed.TotalMilliseconds;
gc();
action(); //JIT
action(); //Optimize
Stopwatch stopwatch2 = Stopwatch.StartNew();
for (int j = 0; j < Iterations; j++)
{
action();
}
gc();
double testElapsed = stopwatch2.Elapsed.TotalMilliseconds;
return (testElapsed - loopElapsed);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2273 次 |
| 最近记录: |