Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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#_Performance - Fatal编程技术网

C# 字符串长度昂贵

C# 字符串长度昂贵,c#,performance,C#,Performance,我有两个字符串值,我想求这两个字符串的长度之和。怎样才能算出最好的办法 第一: string firstStr = "this a first message"; string secondStr = "this a second message"; int total = firstStr.Length + secondStr.Length; 第二: string firstStr = "this a first message"; string secondStr = "this a se

我有两个字符串值,我想求这两个字符串的长度之和。怎样才能算出最好的办法

第一:

string firstStr = "this a first message";
string secondStr = "this a second message";
int total = firstStr.Length + secondStr.Length;
第二:

string firstStr = "this a first message";
string secondStr = "this a second message";
int total = (firstStr + secondStr).Length;

或者其他?

第一个可能更快,因为您的字符串对象已经知道其长度。第二次添加连接操作。

第一次可能更快,因为您的字符串对象已经知道其长度。第二次添加连接操作。

您的第一个选项不会创建新字符串,因此在我的书中它更好


int total=firstStr.Length+secondStr.Length

您的第一个选项不会创建新字符串,因此在我的书中它更好


int total=firstStr.Length+secondStr.Length

第二个版本正在创建一个新的字符串实例,然后获取其长度。它应该很贵。但是,无论区别是什么,对于上述代码行来说都可以忽略不计

第二个版本正在创建一个新的字符串实例,然后获取其长度。它应该很贵。但是,无论区别是什么,对于上述代码行来说都可以忽略不计

第一种方法效率更高,因为它只需添加两个数字

第二种方法是浪费的,因为它创建一个新对象,将两个字符串的内容复制到其中,计算长度,然后丢弃临时对象——这几乎不能有效地使用CPU


比较两者的另一种方法是比较它们的渐近时间——第一个解为O1,第二个解为Om+n。第一次计算在固定时间内完成,因为字符串长度随时可用。第二种计算需要复制每个字符串的内容,这是线性的。

第一种方法效率更高,因为它只需将两个数字相加

第二种方法是浪费的,因为它创建一个新对象,将两个字符串的内容复制到其中,计算长度,然后丢弃临时对象——这几乎不能有效地使用CPU


比较两者的另一种方法是比较它们的渐近时间——第一个解为O1,第二个解为Om+n。第一次计算在固定时间内完成,因为字符串长度随时可用。第二个计算需要复制每个字符串的内容,这是线性的。

第一个计算效率更高,因为它不涉及通过串联创建第三个字符串。
在第一种情况下,您只需将内存中已有的两个对象的长度相加,而不会浪费内存/时间。

第一种方法效率更高,因为它不涉及通过串联创建第三个字符串。
在第一种情况下,您只需将内存中已有的两个对象的长度相加,而不必浪费内存/时间。

我做了两次测试运行,每种方法运行1M次

第一次进近:38189毫秒

第二次进近:504055毫秒

代码如下:

class Program
{
    static void Main(string[] args)
    {
        Stopwatch watch;

        watch = new Stopwatch();
        watch.Start();
        First();
        watch.Stop();
        Trace.WriteLine(string.Format("first: {0} ms",watch.Elapsed.TotalMilliseconds));

        watch = new Stopwatch();
        watch.Start();
        Second();
        watch.Stop();
        Trace.WriteLine(string.Format("second: {0} ms", watch.Elapsed.TotalMilliseconds));
    }

    static void First()
    {
        for (int i = 0; i < 1000000; i++)
        {
            string firstStr = "this a first message";
            string secondStr = "this a second message";
            int total = firstStr.Length + secondStr.Length;
        }

    }

    static void Second()
    {
        for (int i = 0; i < 1000000; i++)
        {
            string firstStr = "this a first message";
            string secondStr = "this a second message";
            int total = (firstStr + secondStr).Length;
        }

    }
}

我做了两次测试运行,每种方法运行1百万次

第一次进近:38189毫秒

第二次进近:504055毫秒

代码如下:

class Program
{
    static void Main(string[] args)
    {
        Stopwatch watch;

        watch = new Stopwatch();
        watch.Start();
        First();
        watch.Stop();
        Trace.WriteLine(string.Format("first: {0} ms",watch.Elapsed.TotalMilliseconds));

        watch = new Stopwatch();
        watch.Start();
        Second();
        watch.Stop();
        Trace.WriteLine(string.Format("second: {0} ms", watch.Elapsed.TotalMilliseconds));
    }

    static void First()
    {
        for (int i = 0; i < 1000000; i++)
        {
            string firstStr = "this a first message";
            string secondStr = "this a second message";
            int total = firstStr.Length + secondStr.Length;
        }

    }

    static void Second()
    {
        for (int i = 0; i < 1000000; i++)
        {
            string firstStr = "this a first message";
            string secondStr = "this a second message";
            int total = (firstStr + secondStr).Length;
        }

    }
}


你关心的是业绩吗?这是一个微观优化。但一般来说,最好避免字符串串联。@KonradKokosa是对的。第二个要慢得多。字符串是不可变的,所以从我的经验来看,第一个字符串的长度绝对不会影响程序的性能。您关心的是什么?这是一个微观优化。但一般来说,最好避免字符串串联。@KonradKokosa是对的。第二个要慢得多。字符串是不可变的,所以从我的经验来看,绝对不需要字符串的长度,因为这会影响程序的性能基准测试微优化非常困难。事实上,它从来没有以这样一种方式来提供关于正在发生的事情的有意义的洞察力。在没有看到任何测试代码的情况下,这实际上是毫无意义的。是的,我知道对微观优化进行基准测试是很困难的。但在这种情况下,测试是相当孤立的,而且只是出于自身利益。测试代码是原始问题中的代码。ms表示毫秒,而不是秒。我添加了示例代码,这样你就可以看到测试的样子。是的,我很清楚这一点,但其中1000毫秒等于一秒,所以我没有说50000毫秒,而是说50秒。好的,对于数字格式的混乱感到抱歉:也许在我的VS输出窗口中使用不同的本地化设置对标微优化确实很困难。事实上,它从来没有以这样一种方式来提供关于正在发生的事情的有意义的洞察力。在没有看到任何测试代码的情况下,这实际上是毫无意义的。是的,我知道对微观优化进行基准测试是很困难的。但在这种情况下,测试是相当孤立的,而且只是出于自身利益。测试代码是原始问题中的代码。ms表示毫秒,而不是秒。我补充说
示例代码,以便您可以看到测试的样子。是的,我很清楚这一点,但其中1000毫秒等于一秒,所以我没有说50000毫秒,而是说50秒。好的,很抱歉数字格式混乱:可能在我的VS输出窗口中有不同的本地化设置