Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Fast string to integer conversion

Fast string to integer conversion

Scheduled Pinned Locked Moved C#
algorithmscom
16 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H HosamAly

    I am parsing a live feed that contains many numbers. The feed comes from a trusted source (i.e. format is guaranteed to be correct), and the numbers are simple ASCII integers (no localization, no thousands separator, etc). Int32.Parse is simply too slow for such "simple" operations. In my benchmarks, it's taking 5-16 times more than some of my implementations.

    My LinkedIn Profile

    J Offline
    J Offline
    J4amieC
    wrote on last edited by
    #7

    So you say you've written a fast implementation of int parsing, that performs better than int.Parse - so what exactly are you asking? If you've done it right? You've not posted your code if so!

    H 1 Reply Last reply
    0
    • J J4amieC

      So you say you've written a fast implementation of int parsing, that performs better than int.Parse - so what exactly are you asking? If you've done it right? You've not posted your code if so!

      H Offline
      H Offline
      HosamAly
      wrote on last edited by
      #8

      I am asking whether there exists a library that includes such functions, in hope that it would be better tested and optimized than my own implementation. If there isn't, I was hoping to get some links to papers or algorithms for how to convert a string to a number efficiently.

      My LinkedIn Profile

      J 1 Reply Last reply
      0
      • H HosamAly

        I am searching for a fast string to integer conversion algorithm. Int32.Parse is too slow for my purposes. I implemented an algorithm that was faster by orders of magnitude, but I was hoping someone could point me to an existing library that includes such functions, or at least to a paper to make sure I'm working correctly. Thank you.

        My LinkedIn Profile

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #9

        Parsing a string is not very hard. I put together this code, it should cover the basics:

        public static int ParseInt32(string text) {
        long value = 0;
        long sign = 1;
        bool first = true;
        foreach (char c in text) {
        if (c >= '0' && c <= '9') {
        value = value * 10 + c - '0';
        } else if (c == '-' && first) {
        sign = -1;
        } else {
        throw new FormatException();
        }
        first = false;
        }
        value *= sign;
        if (value < int.MinValue || value > int.MaxValue) throw new OverflowException();
        return (int)value;
        }

        Despite everything, the person most likely to be fooling you next is yourself.

        N H 2 Replies Last reply
        0
        • H HosamAly

          I am asking whether there exists a library that includes such functions, in hope that it would be better tested and optimized than my own implementation. If there isn't, I was hoping to get some links to papers or algorithms for how to convert a string to a number efficiently.

          My LinkedIn Profile

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #10

          Ok, I see. The performance issue with Int32.Parse is because, like alot of things in the framework, it tries to cater for every man's needs and therefore includes the ability, and logic (therefore overhead) to parse lots of different types of string (eg, currency, hex etc). If you are satisfied that you are only receiving decimal-formatted numbers, without any other markup (such as separators) you could look at Int32.Parse in reflector and follow the trail to the unmanaged code that does the actual parsing - this will lead you into an internal class called Number which has an unsafe method ParseNumber that does the bulk of the work. Your job would be to identify the bit that parses a decimal-formatted number and just extract that portion into your own library.

          1 Reply Last reply
          0
          • G Guffa

            Parsing a string is not very hard. I put together this code, it should cover the basics:

            public static int ParseInt32(string text) {
            long value = 0;
            long sign = 1;
            bool first = true;
            foreach (char c in text) {
            if (c >= '0' && c <= '9') {
            value = value * 10 + c - '0';
            } else if (c == '-' && first) {
            sign = -1;
            } else {
            throw new FormatException();
            }
            first = false;
            }
            value *= sign;
            if (value < int.MinValue || value > int.MaxValue) throw new OverflowException();
            return (int)value;
            }

            Despite everything, the person most likely to be fooling you next is yourself.

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #11

            That was brilliant and simple. 5ed

            Navaneeth How to use google | Ask smart questions

            L G 2 Replies Last reply
            0
            • N N a v a n e e t h

              That was brilliant and simple. 5ed

              Navaneeth How to use google | Ask smart questions

              L Offline
              L Offline
              Le centriste
              wrote on last edited by
              #12

              You could also disable array bound checking, which could be even faster. Check the documentation for "unsafe" and "fixed" C# keywords.

              H 1 Reply Last reply
              0
              • N N a v a n e e t h

                That was brilliant and simple. 5ed

                Navaneeth How to use google | Ask smart questions

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #13

                N a v a n e e t h wrote:

                That was brilliant and simple.

                Thanks a lot. :)

                Despite everything, the person most likely to be fooling you next is yourself.

                1 Reply Last reply
                0
                • G Guffa

                  Parsing a string is not very hard. I put together this code, it should cover the basics:

                  public static int ParseInt32(string text) {
                  long value = 0;
                  long sign = 1;
                  bool first = true;
                  foreach (char c in text) {
                  if (c >= '0' && c <= '9') {
                  value = value * 10 + c - '0';
                  } else if (c == '-' && first) {
                  sign = -1;
                  } else {
                  throw new FormatException();
                  }
                  first = false;
                  }
                  value *= sign;
                  if (value < int.MinValue || value > int.MaxValue) throw new OverflowException();
                  return (int)value;
                  }

                  Despite everything, the person most likely to be fooling you next is yourself.

                  H Offline
                  H Offline
                  HosamAly
                  wrote on last edited by
                  #14

                  Thanks. I was actually asking whether such functionality initially existed. Meanwhile, I wrote some functions (similar to yours but not exactly the same), which you can check here[^].

                  My LinkedIn Profile

                  1 Reply Last reply
                  0
                  • L Le centriste

                    You could also disable array bound checking, which could be even faster. Check the documentation for "unsafe" and "fixed" C# keywords.

                    H Offline
                    H Offline
                    HosamAly
                    wrote on last edited by
                    #15

                    Sometimes yes, sometimes no. According to my measurements, sometimes the cost of using fixed is almost the same as bounds checking (especially for small arrays), because it calls a function to pin the referred object and return a pointer to it. (You can check it with IL DASM.)

                    My LinkedIn Profile

                    L 1 Reply Last reply
                    0
                    • H HosamAly

                      Sometimes yes, sometimes no. According to my measurements, sometimes the cost of using fixed is almost the same as bounds checking (especially for small arrays), because it calls a function to pin the referred object and return a pointer to it. (You can check it with IL DASM.)

                      My LinkedIn Profile

                      L Offline
                      L Offline
                      Le centriste
                      wrote on last edited by
                      #16

                      Absolutely. One must choose the solution that fits best, that is why testing more than one way to achieve this is the best way to find out.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups