在 C# 中,哪一个更快? Convert.ToString()、(string)casting 还是 .ToString()?

发布于 2024-07-22 05:01:31 字数 153 浏览 10 评论 0原文

在 C# 中,哪个更快?

  • System.Convert.ToString(objThatIsAString)
  • (字符串)objThatIsAString
  • objThatIsAString.ToString()

In C#, which one is faster?

  • System.Convert.ToString(objThatIsAString)
  • (string)objThatIsAString
  • objThatIsAString.ToString()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

2024-07-29 05:01:31

@蒂斯:
这是一个快速测试:

public class ToStringTest
{
    private object mString = "hello world!";
    Stopwatch sw = new Stopwatch();
    private List<long> ConvertToStringTimes = new List<long>();
    private List<long> ToStringTimes = new List<long>();
    private List<long> CastToStringTimes = new List<long>();

    public ToStringTest()
    {

        for (int i = 0; i < 100000; i++)
        {
            sw.Start();
            ConvertToString();
            sw.Stop();
            ConvertToStringTimes.Add(sw.ElapsedTicks);
            sw.Reset();

            sw.Start();
            ToString();
            sw.Stop();
            ToStringTimes.Add(sw.ElapsedTicks);
            sw.Reset();

            sw.Start();
            CastToString();
            sw.Stop();
            CastToStringTimes.Add(sw.ElapsedTicks);
            sw.Reset();    
        }
        Console.WriteLine("Average elapsed ticks after converting {0} strings",ConvertToStringTimes.Count);
        Console.WriteLine("ConvertToString: {0} ticks", ConvertToStringTimes.Average() );
        Console.WriteLine("ToString: {0} ticks", ToStringTimes.Average());
        Console.WriteLine("CastToString: {0} ticks",  CastToStringTimes.Average());
    }

    private string ConvertToString()
    {
        return Convert.ToString(mString);
    }

    public override string ToString()
    {
        return mString.ToString();
    }

    public string CastToString()
    {
        return (string) mString;
    }
}

结果:

转换 100000 个字符串后平均经过的刻度数

ConvertToString:611.97372 刻度数

ToString:586.51461 刻度数

CastToString:582.25266 刻度数

@thijs:
Here's a quick test:

public class ToStringTest
{
    private object mString = "hello world!";
    Stopwatch sw = new Stopwatch();
    private List<long> ConvertToStringTimes = new List<long>();
    private List<long> ToStringTimes = new List<long>();
    private List<long> CastToStringTimes = new List<long>();

    public ToStringTest()
    {

        for (int i = 0; i < 100000; i++)
        {
            sw.Start();
            ConvertToString();
            sw.Stop();
            ConvertToStringTimes.Add(sw.ElapsedTicks);
            sw.Reset();

            sw.Start();
            ToString();
            sw.Stop();
            ToStringTimes.Add(sw.ElapsedTicks);
            sw.Reset();

            sw.Start();
            CastToString();
            sw.Stop();
            CastToStringTimes.Add(sw.ElapsedTicks);
            sw.Reset();    
        }
        Console.WriteLine("Average elapsed ticks after converting {0} strings",ConvertToStringTimes.Count);
        Console.WriteLine("ConvertToString: {0} ticks", ConvertToStringTimes.Average() );
        Console.WriteLine("ToString: {0} ticks", ToStringTimes.Average());
        Console.WriteLine("CastToString: {0} ticks",  CastToStringTimes.Average());
    }

    private string ConvertToString()
    {
        return Convert.ToString(mString);
    }

    public override string ToString()
    {
        return mString.ToString();
    }

    public string CastToString()
    {
        return (string) mString;
    }
}

Results in:

Average elapsed ticks after converting 100000 strings

ConvertToString: 611.97372 ticks

ToString: 586.51461 ticks

CastToString: 582.25266 ticks

老娘不死你永远是小三 2024-07-29 05:01:31

除了运行时类型检查之外,直接转换不需要进行任何检查 - 我期望转换速度更快。

您可能还想考虑 objThatIsString.ToString() ; 因为(对于string)这只是return this;,它应该很快 - 当然比Convert.ToString更快。 然后,竞争就发生在运行时类型检查和虚拟调用之间; 事实上,两者都非常非常快。

The direct cast doesn't have to do any checking except a runtime type check - I would expect that the cast is quicker.

You might also want to consider objThatIsString.ToString(); since (for string) this is just return this;, it should be quick - certainly quicker than the Convert.ToString. Then the race is between a runtime type-check and a virtual call; in reality, both are very, very quick.

能否归途做我良人 2024-07-29 05:01:31

演员阵容更快。

Convert.ToString 最终将在一些开销后进行转换。 它实际上是通过尝试将其转换为 IConvertible,然后调用其虚拟方法 ToString 的形式来实现的。 因此,实际上是虚拟调用将其转换为String

The cast is faster.

The Convert.ToString will eventually do a cast after some overhead. It actually does it in the form of attempting to cast it to IConvertible, and then call the virtual method ToString on it. So, it's the virtual call that does the actual casting to String.

故人如初 2024-07-29 05:01:31

转换 (string)obj 应该更快。 Convert 类实际上转换不同类的对象,在这种情况下会比较慢。

The cast (string)obj should be faster. The Convert class actually converts objects that are of different class and will be slower in this case.

み青杉依旧 2024-07-29 05:01:31

有句话说“数字说明故事”。 这意味着您不仅可以假设某些东西,还可以测量它!

因此,打包一个测试应用程序,运行测试并验证结果!

真正的问题可能是:
如何衡量哪种方式更快?

There's a saying "The numbers tell the tale". Which means that instead of assuming something you can also measure it!

So, wrap up a test app, run your tests and validate the results!

The true question could be:
How do I measure which way is faster?

幸福不弃 2024-07-29 05:01:31

我认为 (string) objThatIsString 更快,因为编译器将能够在编译时进行此转换。

然而,Jeff Atwood 认为这毕竟不重要 (编码恐怖:微的悲伤悲剧-优化剧场)

I think that (string) objThatIsString is faster because the Compiler will be able to do this conversion at compile time.

However, Jeff Atwood thinks that its not important after all (Coding Horror: The Sad Tragedy of Micro-Optimization Theater)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文