RE: Check value is number only

Tech-Archive recommends: Fix windows errors by optimizing your registry



Brendan Grant <grantb@xxxxxxxxxxxxxxx> wrote:
> Code readability is always good, however there are times when performance is
> more important than readability, and with extensive testing one can
> dramatically reduce the risk of such a complicated regular expression from
> causing you issues.
>
> While using a Parse() may be simpler and more readable, I disagree that it
> is more efficient.

I don't think you read my post properly. I wasn't suggesting *just*
using int.Parse - I was suggesting a simple method to test for just
digits (without using a regular expression) followed by a call to
Parse.

> Usage of either depends on which side you want to pay the
> penalty of an unparseable value.
>
> If it is a parseable value being passed in, then yes, Parse() faster, an
> order of magnitude faster in the best case. On the other hand, if a lot of
> invalid values are being dealt with, using the Regex method can be in the
> best case ~50 times faster than Parse().

Whereas a hard-coded test followed by int.Parse can be about 50x faster
than Regex.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: Need help with regular expression to parse URLs
    ... I am having trouble figuring out how to write a regular expression to ... I am trying to parse the url ... Neil Aggarwal, 846-8957, www.JAMMConsulting.com ... Unlimited Access, Anonymous Accounts, Uncensored Broadband Access ...
    (comp.lang.java.programmer)
  • Re: How to extract variable and its value from URL
    ... You wouldn't even need a regular expression, you can just parse it apart ... on the equals sign and the ampersand (but a regular expression would work ... property to get the query string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: parse a file into text file
    ... I have to parse this text file. ... You might be able to write a regular expression that will satisfactorily break the data into pieces. ... If and when your employer finds out how careless you are with what is clearly sensitive information, you will be fortunate if you are simply removed from that project. ... Your best bet at the moment is to confess to your employer so that you can begin "damage control", including notification of the doctor and their patients to alert them to the fact that the data has been publicly released. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Check value is number only
    ... Code readability is always good, however there are times when performance is ... dramatically reduce the risk of such a complicated regular expression from ... While using a Parse() may be simpler and more readable, ... > They also don't perform as well as "brute force" hard coded tests in ...
    (microsoft.public.dotnet.languages.csharp)
  • Need help with regular expression to parse URLs
    ... I am having trouble figuring out how to write a regular expression to ... I am trying to parse the url ... Pattern I apply to the group after I extract it from the main url? ...
    (comp.lang.java.programmer)