Efficient way to do this

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Blair

    Efficient way to do this

    Hi,

    I get passed a string like these examples:

    3.1
    4.23
    1.1
    5

    I need to seperate the values into two integer variables.
    So 3.1 would become

    int var1, var2;

    //var1 should have 3
    //var2 should have 1

    if it was 4.23

    //var1 should have 4
    //var2 should have 23

    This is will be on a part of the system, where performance is critical
    and need an efficient method for processing strings like this.

    Thanks for the help in advance.

    Steven


    *** Sent via Developersdex http://www.developersdex.com ***
  • Marc Gravell

    #2
    Re: Efficient way to do this

    How about something like:
    private static int[] Parse(string value) {
    string[] elements = value.Split('.' );
    int length = elements.Length ;
    int[] values = new int[length];
    for (int i = 0; i < length; i++) {
    values[i] = int.Parse(eleme nts[i]);
    }
    return values;
    }

    If you know there are never more than 2 parts, then you might be able to
    tweak bits of it, but doubt it would be worth it...

    Marc


    Comment

    • Laura T

      #3
      Re: Efficient way to do this

      Efficient depends on various parameters like speed, memory usage ecc.
      I'd use something like this:

      string sval = "44.55";
      int v1, v2;

      int s = sval.IndexOf('. '); // linear but small strings
      if (s < 0) // Case no decimals
      {
      v1 = Convert.ToInt32 (sval); // full integer
      }
      else
      {
      v1 = Convert.ToInt32 (sval.Substring (0, s)); // temp string
      (fast no gc)
      v2 = Convert.ToInt32 (sval.Substring (s + 1)); // idem
      }


      "Steven Blair" <steven.blair@b tinternet.comha scritto nel messaggio
      news:ehHRrsd8GH A.4476@TK2MSFTN GP04.phx.gbl...
      Hi,
      >
      I get passed a string like these examples:
      >
      3.1
      4.23
      1.1
      5
      >
      I need to seperate the values into two integer variables.
      So 3.1 would become
      >
      int var1, var2;
      >
      //var1 should have 3
      //var2 should have 1
      >
      if it was 4.23
      >
      //var1 should have 4
      //var2 should have 23
      >
      This is will be on a part of the system, where performance is critical
      and need an efficient method for processing strings like this.
      >
      Thanks for the help in advance.
      >
      Steven
      >
      >
      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Christof Nordiek

        #4
        Re: Efficient way to do this

        Hi Laura

        why do you say 'no gc' in the line with Substring method. The Substring
        method surely creates a new string-instance on the GC-Heap.
        To avoid this string instances, one has also to avoid the Convert class and
        the Parse method at all. It could be possible, to access the single chars
        from the string and calculate the values directly.
        But i'm not sure if this really matters. To know this one has to make
        performance tests.

        "Laura T" <laura_t_@yahoo .comschrieb im Newsbeitrag
        news:ekl6ile8GH A.3820@TK2MSFTN GP02.phx.gbl...
        Efficient depends on various parameters like speed, memory usage ecc.
        I'd use something like this:
        >
        string sval = "44.55";
        int v1, v2;
        >
        int s = sval.IndexOf('. '); // linear but small strings
        if (s < 0) // Case no decimals
        {
        v1 = Convert.ToInt32 (sval); // full integer
        }
        else
        {
        v1 = Convert.ToInt32 (sval.Substring (0, s)); // temp
        string (fast no gc)
        v2 = Convert.ToInt32 (sval.Substring (s + 1)); // idem
        }
        >
        >
        "Steven Blair" <steven.blair@b tinternet.comha scritto nel messaggio
        news:ehHRrsd8GH A.4476@TK2MSFTN GP04.phx.gbl...
        >Hi,
        >>
        >I get passed a string like these examples:
        >>
        >3.1
        >4.23
        >1.1
        >5
        >>
        >I need to seperate the values into two integer variables.
        >So 3.1 would become
        >>
        >int var1, var2;
        >>
        >//var1 should have 3
        >//var2 should have 1
        >>
        >if it was 4.23
        >>
        >//var1 should have 4
        >//var2 should have 23
        >>
        >This is will be on a part of the system, where performance is critical
        >and need an efficient method for processing strings like this.
        >>
        >Thanks for the help in advance.
        >>
        >Steven
        >>
        >>
        >*** Sent via Developersdex http://www.developersdex.com ***
        >
        >

        Comment

        • Adityanand Pasumarthi

          #5
          RE: Efficient way to do this

          Hi Steven,

          Here is my solution...

          long st = DateTime.Now.Ti cks;
          for(int i = 1; i <= 1000000; i++)
          {
          string sval = "4.23";
          int v1 = 0;
          int v2 = 0;
          int s = sval.IndexOf('. ');
          if (s < 0)
          {
          int len = sval.Length;
          for(int j = 0; j < len; j++) v1 = (v1 * 10) + ((int) sval[j] - 48);
          }
          else
          {
          int len = sval.Length;
          for(int j = 0; j < s; j++) v1 = (v1 * 10) + ((int) sval[j] - 48);
          for(int j = s + 1; j < len; j++) v2 = (v2 * 10) + ((int) sval[j] - 48);
          }
          //Console.WriteLi ne(v1);
          //Console.WriteLi ne(v2);
          }
          long et = DateTime.Now.Ti cks;
          Console.WriteLi ne("Time: {0}",(et - st)/10000);

          My approach has 0 Gen 0 collections, no heap memory and 10 times (1000%)
          faster than other 2 solutions. Here are the stats taken from Windows Profiler
          my 1.7 GHz single processor WIn 2K machine with 1 GB RAM.

          Parse static method
          -------------------------
          445678.989 - 1 million iterations took 2300 milliseconds - 200 (Gen 0
          collections) - 62 KB (Bytes in all Heaps)

          445678 - 1 million iterations took 1200 milliseconds - 90 (Gen 0
          collections) - 62 KB (Bytes in all Heaps)

          4.4 - 1 million iterations took 1800 milliseconds - 200 (Gen 0 collections)
          - 62 KB (Bytes in all Heaps)

          SubString
          ------------
          445678.989 - 1 million iterations took 1400 milliseconds - 80 (Gen 0
          collections) - 32 KB (Bytes in all Heaps)

          445678 - 1 million iterations took 600 milliseconds - 0 (Gen 0 collections)
          - 32 KB (Bytes in all Heaps)

          4.4 - 1 million iterations took 1200 milliseconds - 80 (Gen 0 collections) -
          32 KB (Bytes in all Heaps)

          My approach
          ----------------
          445678.989 - 1 million iterations took 240 milliseconds - 0 (Gen 0
          collections) - 0 KB (Bytes in all Heaps)

          445678 - 1 million iterations took 175 milliseconds - 0 (Gen 0 collections)
          - 0 KB (Bytes in all Heaps)

          4.4 - 1 million iterations took 95 milliseconds - 0 (Gen 0 collections) - 0
          KB (Bytes in all Heaps)

          This is with .Net 1.1. I'll post you my results for .Net 2.0 soon.

          Regards,
          Aditya.P

          "Steven Blair" wrote:
          Hi,
          >
          I get passed a string like these examples:
          >
          3.1
          4.23
          1.1
          5
          >
          I need to seperate the values into two integer variables.
          So 3.1 would become
          >
          int var1, var2;
          >
          //var1 should have 3
          //var2 should have 1
          >
          if it was 4.23
          >
          //var1 should have 4
          //var2 should have 23
          >
          This is will be on a part of the system, where performance is critical
          and need an efficient method for processing strings like this.
          >
          Thanks for the help in advance.
          >
          Steven
          >
          >
          *** Sent via Developersdex http://www.developersdex.com ***
          >

          Comment

          Working...