超载或可选参数之间的性能差异? [英] Performance differences between overloading or optional parameters?

查看:113
本文介绍了超载或可选参数之间的性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有,没有回答有↑

我不知道是否应该在C#中使用可选参数。到现在为止我一直重载方法。但可选参数都不错过,更清洁,更少的代码。我用他们在其他语言,所以我也以某种方式使用它们。有什么,讲反对使用它们?性能是我的第一个关键点。难道降?



示例代码:

I wonder if I should be using optional parameters in C#. Until now I was always overloading methods. But optional parameters are nice too, cleaner and less code. And I use them in other languages so I'm also used to them in some way. Is there anything that speaks against using them ? Performance is the first key point for me. Would it drop ?


Example code:

class Program
{
    // overloading
    private static void test1(string text1)
    {
        Console.WriteLine(text1 + " " + "world");
    }

    private static void test1(string text1, string text2)
    {
        Console.WriteLine(text1 + " " + text2);
    }

    // optional parameters
    private static void test2(string text1, string text2 = "world")
    {
        Console.WriteLine(text1 + " " + text2);
    }

    static void Main(string[] args)
    {
        test1("hello");
        test1("hello", "guest");

        test2("hello"); // transforms to test2("hello", "world"); ?
        test2("hello", "guest");

        Console.ReadKey();
    }
}





我测量的时间需要几百万超载呼叫和可选参数呼叫。


I measured the time needed for a few millions overload calls and optional parameter calls.


  • 可选参数与字符串:18%的(2个参数,发布)

  • 可选参数与整数:其中1%的更快(2个参数,发布)

  • optional parameters with strings: 18 % slower (2 parameters, release)
  • optional parameters with ints: <1 % faster (2 parameters, release)

(也许编译器优化或优化未来的可选参数调用?)

(And maybe compilers optimize or will optimize those optional parameter calls in future ?)

推荐答案

我只是做了一个快速的测试和编译器优化代码。这个基本的例子主要方法。

I just did a quick test and the compiler optimizes the code. This basic example Main method.

public static void OptionalParamMethod(bool input, string input2 = null)
{
}

public static void Main(string[] args)
{
    OptionalParamMethod(true);
    OptionalParamMethod(false, "hello");
}



编译该所以可选PARAMS是由编译器填充。

Compiles to this so the optional params are filled in by the compiler.

public static void Main(string[] args)
{
    OptionalParamMethod(true, null);
    OptionalParamMethod(false, "hello");
}



至于性能,你可以争辩可选参数有微弱的优势,因为只有一个方法调用,而不是链接的方法调用就像你通常有一个重载的方法。下面的代码被编译输出显示什么我谈论。该差异是相当的学术,虽然我怀疑你会在实践中注意到。

As for performance you could argue optional parameters have a slight advantage as there is only a single method call rather than chained method calls like you would normally have for an overloaded method. The code below is compiled output to show what I am talking about. The difference is quite academic though and I doubt you would ever notice in practice.

public static void Main(string[] args)
{
    OptionalParamMethod(true, null);
    OptionalParamMethod(false, "hello");
    OverloadParamMethod(true);
    OverloadParamMethod(false, "hello");
}

public static void OptionalParamMethod(bool input, [Optional, DefaultParameterValue(null)] string input2)
{
    Console.WriteLine("OptionalParamMethod");
}

public static void OverloadParamMethod(bool input)
{
    OverloadParamMethod(input, null);
}

public static void OverloadParamMethod(bool input, string input2)
{
    Console.WriteLine("OverloadParamMethod");
}

这篇关于超载或可选参数之间的性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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