Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 哪种返回视图的方法在性能方面是最好的?_C#_Asp.net Mvc - Fatal编程技术网

C# 哪种返回视图的方法在性能方面是最好的?

C# 哪种返回视图的方法在性能方面是最好的?,c#,asp.net-mvc,C#,Asp.net Mvc,将视图返回控制器操作的最佳方法是什么?在性能、易于测试、优化等方面 return View(); 或 然后 return View([model]); vs 所以我运行了一个小基准测试,这就是我得到的: readonly Stopwatch sw = new Stopwatch(); public void Benchmark() { var t1 = Test(View); var t2 = Test(() => View("

将视图返回控制器操作的最佳方法是什么?在性能、易于测试、优化等方面

return View();

然后

return View([model]);
vs


所以我运行了一个小基准测试,这就是我得到的:

    readonly Stopwatch sw = new Stopwatch();

    public void Benchmark()
    {
        var t1 = Test(View);
        var t2 = Test(() => View("[ActionName]"));
        var t4 = Test(() => View(new Model()));
        var t3 = Test(() => View("[ActionName]", new Model()));

        string result = $"{t1} - {t2} - {t3} - {t4}";
        //Results:
        //4466 - 4856 - 6969 - 6977
        //4551 - 4986 - 7070 - 7056
        //5181 - 5263 - 7142 - 7864

    }

    public long Test(Func<ViewResult> f)
    {
        sw.Start();
        for (int i = 0; i < 100000000; i++)
        { var x = f(); }

        sw.Stop();
        long t = sw.ElapsedMilliseconds;
        sw.Reset();
        return t;
    }
readonly秒表sw=新秒表();
公共服务基准()
{
var t1=测试(视图);
var t2=测试(()=>视图(“[ActionName]”);
var t4=测试(()=>视图(新模型());
var t3=Test(()=>View(“[ActionName]”,newmodel());
字符串结果=$“{t1}-{t2}-{t3}-{t4}”;
//结果:
//4466 - 4856 - 6969 - 6977
//4551 - 4986 - 7070 - 7056
//5181 - 5263 - 7142 - 7864
}
公共长测试(Func f)
{
sw.Start();
对于(int i=0;i<100000000;i++)
{var x=f();}
sw.Stop();
长t=sw.ElapsedMilliseconds;
sw.Reset();
返回t;
}

所以前两个看起来快一点。。。然而,差别是微不足道的。使用最适合您的方法。

StackOverflow不是一个建议论坛。你想在这里完成什么?与性能相关的任何事情,都要自己测试。分析您的代码以查看是否有任何显著差异。我真的怀疑有多少差异。即使有任何差异,差异也会比总请求执行时间小几个数量级。使用您需要的最简单的重载;
return View("[ActionName]",[model]);
    readonly Stopwatch sw = new Stopwatch();

    public void Benchmark()
    {
        var t1 = Test(View);
        var t2 = Test(() => View("[ActionName]"));
        var t4 = Test(() => View(new Model()));
        var t3 = Test(() => View("[ActionName]", new Model()));

        string result = $"{t1} - {t2} - {t3} - {t4}";
        //Results:
        //4466 - 4856 - 6969 - 6977
        //4551 - 4986 - 7070 - 7056
        //5181 - 5263 - 7142 - 7864

    }

    public long Test(Func<ViewResult> f)
    {
        sw.Start();
        for (int i = 0; i < 100000000; i++)
        { var x = f(); }

        sw.Stop();
        long t = sw.ElapsedMilliseconds;
        sw.Reset();
        return t;
    }