Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 101767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:50:05+00:00 2026-05-11T00:50:05+00:00

I am writing a high performance parser, and it seems to me that Int32.Parse

  • 0

I am writing a high performance parser, and it seems to me that Int32.Parse can be too slow. I wrote a simple version that assumes correct input, and it’s performing much better. So should I create my own version instead? Or is there another faster method already available?

My method is like this:

// parse simple int, assuming relatively correct input (i.e. all digits) public static int ParseInt32Simply(string str) {     if (str == null) throw new ArgumentNullException('str');     if (str.Length == 0) throw new ArgumentException('str is empty');      int sign = 1, index = 0;     if (str[0] == '-') { sign = -1; index = 1; }     else if (str[0] == '+') { index = 1; }      int result = 0;     for (; index < str.Length; ++index) {         result = 10 * result + (str[index] - '0');     }      if (result < 0) throw new OverflowException(str + ' is too large for Int32');      return result * sign; } 

My results are very different from the builtin equivalent:

Int32.Parse      took 8.2775453 seconds ParseInt32Simply took 0.6511523 seconds Int32.Parse      took 6.7625807 seconds ParseInt32Simply took 0.4677390 seconds 

(Running 25 million iterations on my machine; a P4 3 GHz, running VS 2008 SP1)

So, should I use my version? Or is there another method available that I can use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:50:05+00:00Added an answer on May 11, 2026 at 12:50 am

    If your parsing a format of which you know to be valid numbers, you can indeed write a faster custom parser. I’ve written a Double.Parse function for the same purpose once. And it faster to begin with the least significant digit. That way you can just increment the power of the digit your parsing.

    I’ve created a quick implementation of this,

    public static Int32 ParseValidNumberAsInt32(string str) {     if (str == null)          throw new ArgumentNullException('str');     if (str.Length == 0)          throw new ArgumentException('str is empty');     Int32 result = 0;     Int32 currentPower = 1;     Boolean isNegative = str[0] == '-';      for (int currentCharIndex = str.Length - 1; currentCharIndex > 0; currentCharIndex--)     {         result += (str[currentCharIndex] - '0') * currentPower;         currentPower *= 10;     }     return isNegative ? -1 * result : result + ((str[0] - '0') * currentPower); } 

    If you really want speed, you can write a unsafe implementation..

    If your parsing a big file, you could read the files as raw bytes and work with those. That will make it a lot faster (no converting to unicode string, no splitting the strings in lines, no splitting the lines in substrings, no parsing the substrings), but you’re going to lose maintainability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The MemFree alone says nearly nothing. Linux's block layer caches… May 12, 2026 at 4:11 pm
  • Editorial Team
    Editorial Team added an answer Yes, when you actually edit the CSS there is a… May 12, 2026 at 4:11 pm
  • Editorial Team
    Editorial Team added an answer I think your problem may be that you are neglecting… May 12, 2026 at 4:11 pm

Related Questions

I'm writing a straightforward C program on Linux and wish to use an existing
I am trying to improve the performance of the threaded application with real-time deadlines.
I'm writing a simulator in Python, and am curious about options and opinions regarding
I have developed a quite large application using MFC. Naturaly, I used GDI for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.