本文介绍了字符串列表<双>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 System.String 的由一系列用空格或逗号隔开,像这样的实数的:

I have a System.String composed of a series of real numbers separated by spaces or commas, like this:

645.974,255.478 645.973,255.468 645.97 0 233.54 21

我想分析我的字符串和填充的其号码。我试图找到执行此任务的最快方式。到目前为止,我已经尝试下面的方法,具有双重的列表,并解析字符串十万次。

I want to parse my string and fill a list with its numbers. I'm trying to find the quickest way to perform this task. So far I have tried the following methods, having a list of double and parsing the string hundred thousand times.

List<double> MyList = new List<double>(250);

  • 正则表达式 :〜8.56 [S]

    • Regex: ~8.56[s]

      MyList.Clear();
      
      foreach (Match match in RgxDouble.Matches(Points))
              MyList.Add(double.Parse(match.Value));
      

    • 未管理code与C ++ / CLI:〜7.5 [S]

    • Unmanaged code with C++/CLI: ~7.5[s]

      static void UsingUnmanagedCode(wchar_t* points, List<double> ^MyList)
      {
          static char buffer[MAX_CHARS];
          wcstombs(buffer, points, MAX_CHARS);
          char *start = buffer, *ptr = buffer;
      
          for(; *ptr; ++ptr)
          {
              if(*ptr == ',')
                  *ptr = ' ';
          }
      
          while(*(--ptr) == ' ')
              *ptr = '\0';
      
          char *pEnd;
          MyList->Add(strtod(start, &pEnd));
      
          while(*pEnd)
          {
              char *pStart = pEnd;
              MyList->Add(strtod(pStart, &pEnd));
          }
      }
      

    • 不安全code与C#和的:〜2.4 [S]

    • Unsafe code with C# and StringBuilder: ~2.4[s]

      MyList.Clear();
      
          fixed (char* fixPointsPtr = Points)
          {
              char* ptr = fixPointsPtr;
      
              while (*ptr != '\0')
              {
                  if ((*ptr < '0' || *ptr > '9') && *ptr != '-' && *ptr != '.')
                  {
                      MyList.Add(double.Parse(strBuild.ToString(0, strBuild.Length), NumberStyles.AllowDecimalPoint));
                      strBuild.Length = 0;
                  }
      
                  else
                      strBuild.Append(*ptr);
      
                  ++ptr;
              }
          }
      

    • 字符串分割:〜2 [S]

    • String Split: ~2[s]

      MyList.Clear();
      
      foreach (string point in Points.Split(Separators, StringSplitOptions.RemoveEmptyEntries))
              MyList.Add(double.Parse(point, NumberStyles.AllowDecimalPoint));
      

    • 有一个更好的选择吗?

      推荐答案

      您可能需要评估你的程序的设计,看看是否可以飞,如果速度是你重要的分割你的字符串。

      You may want to assess the design of your program to see if you can split your string "on the fly" if speed is critical to you.

      例如,请问您的字符串进入你的计划吗?为了让所有的算法超高效率,最好的办法(据我所知)是尝试和凝结在运营成一个圈......单回路比多次循环自动快。一个简单的方法来做到这一点是听为您的数据,因为它进来,烧焦成炭,逐字节,增加了有效的实数到列表中,只要你知道你有一个。

      For example, how does your string come into your program? To make any algorithms ultra efficient, your best bet (as far as I know) is to try and condense your operations into a single loop... a single loop is automatically faster than multiple loops. An easy way to do this is to "listen" for your data as it comes in, char by char, byte by byte, adding a valid real number to your list as soon as you realize you have one.

      这篇关于字符串列表&LT;双&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:08