Thread: Psuedocode

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by artistunknown View Post
    cstryx I'm curious why you say Regular Expressions are slow my book acts like Regular Expressions are very useful but I still like your approach to it
    They are useful for pattern matching, but the overhead of using regex in this case is going to be significantly slower. It may only be faster if you are using compiled regex on a much larger piece of string data.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by cstryx View Post
    They are useful for pattern matching, but the overhead of using regex in this case is going to be significantly slower. It may only be faster if you are using compiled regex on a much larger piece of string data.
    I wouldn't say "significantly" slower, since either of them is fast enough to be effectively instantaneous to human perception.

    Personally, as long as I'm not using a wildly inappropriate algorithm (like an O(N^3) linked list removal or something) my focus is always on readable, maintainable code. If there are actually performance concerns, then I'll rewrite if necessary, but in a lot of cases, the actual processing takes tiny fractions of a second and then idles waiting for user input. Whether it takes 100 nanoseconds or 10,000 nanoseconds, it's still effectively instantaneous to human perception, which finds anything less than about 100 microseconds to be "immediate".

    The printing to the console is going to take orders of magnitude longer than either the regular expression or the character-based pattern matching.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by Cat View Post
    I wouldn't say "significantly" slower, since either of them is fast enough to be effectively instantaneous to human perception.

    Personally, as long as I'm not using a wildly inappropriate algorithm (like an O(N^3) linked list removal or something) my focus is always on readable, maintainable code. If there are actually performance concerns, then I'll rewrite if necessary, but in a lot of cases, the actual processing takes tiny fractions of a second and then idles waiting for user input. Whether it takes 100 nanoseconds or 10,000 nanoseconds, it's still effectively instantaneous to human perception, which finds anything less than about 100 microseconds to be "immediate".

    The printing to the console is going to take orders of magnitude longer than either the regular expression or the character-based pattern matching.
    Significantly slower was a measurement of performance in context with my comparison between simple string manipulation and regex itself, which has nothing to do with human perception. Also, it's not clarified as to whether he needs to validate a bunch of ID's or just a single one, so performance may or may not be accumulative here. Still, regex is not the proper choice IMHO; it wouldn't be mine.

    It's a string of 6 characters in length. You wouldn't turn the barbecue back on to heat up leftover chicken the day after, you would probably just use the microwave. Same principle applies here.

    Code:
    public static bool ValidID(string id)
    {
      return id.Length == 6
            && char.IsLetter(id[0]) && char.IsLetter(id[1])
            && id.Skip(2).All(char.IsDigit);
    }
    You could parse the last 4 chars as an int and see if it was successful, and index[0] and [1] to check them for letters. Many ways to get the job done and still be faster but just as readable as regex.
    Last edited by cstryx; 08-29-2015 at 09:54 PM.

Popular pages Recent additions subscribe to a feed