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. Is Regex faster?

Is Regex faster?

Scheduled Pinned Locked Moved C#
regextutorialquestion
7 Posts 5 Posters 0 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.
  • E Offline
    E Offline
    error1408
    wrote on last edited by
    #1

    Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"

    I C D P 4 Replies Last reply
    0
    • E error1408

      Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"

      I Offline
      I Offline
      Ilya Verbitskiy
      wrote on last edited by
      #2

      May be Regex, which compiled into assembly, but I'm not sure. It's better to test.

      1 Reply Last reply
      0
      • E error1408

        Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        error1408 wrote:

        if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"

        In this case RegEx would be the only one that works. The second example would only ever return false.


        Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

        1 Reply Last reply
        0
        • E error1408

          Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          string.IndexOf


          [My Blog]
          "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - RĂ¼diger Klaehn
          "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          1 Reply Last reply
          0
          • E error1408

            Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Why not try it out for yourself and find out. Create a document containing the phrase "The quick brown fox jumps over the lazy fox" by cutting and pasting repeatedly; it won't take you long to knock up a 50000 line document with this in. Now, put a seed word in the middle somewhere - call it seed for instance. Then, run a program that uses both versions against this document and see which method is faster.

            Deja View - the feeling that you've seen this post before.

            E 1 Reply Last reply
            0
            • P Pete OHanlon

              Why not try it out for yourself and find out. Create a document containing the phrase "The quick brown fox jumps over the lazy fox" by cutting and pasting repeatedly; it won't take you long to knock up a 50000 line document with this in. Now, put a seed word in the middle somewhere - call it seed for instance. Then, run a program that uses both versions against this document and see which method is faster.

              Deja View - the feeling that you've seen this post before.

              E Offline
              E Offline
              error1408
              wrote on last edited by
              #6

              Ok i wrote a little app that compares the two methods. I found out, that testing a file with 60000 lines 30 times took my pc about a second with this method: using (StreamReader rIn = new StreamReader("test.txt")) { string line = ""; while ((line = rIn.ReadLine()) != null) { if (line.Trim() == "{{{tcl") { return true; } } rIn.Close(); } return false; and about 9 seconds with the regex: using (StreamReader rIn = new StreamReader("test.txt")) { string line = ""; Regex reg = new Regex(@"\s*\{\{\{tcl\s*"); while ((line = rIn.ReadLine()) != null) { if (reg.IsMatch(line)) { return true; } } rIn.Close(); } return false; Thats a BIG difference.

              P 1 Reply Last reply
              0
              • E error1408

                Ok i wrote a little app that compares the two methods. I found out, that testing a file with 60000 lines 30 times took my pc about a second with this method: using (StreamReader rIn = new StreamReader("test.txt")) { string line = ""; while ((line = rIn.ReadLine()) != null) { if (line.Trim() == "{{{tcl") { return true; } } rIn.Close(); } return false; and about 9 seconds with the regex: using (StreamReader rIn = new StreamReader("test.txt")) { string line = ""; Regex reg = new Regex(@"\s*\{\{\{tcl\s*"); while ((line = rIn.ReadLine()) != null) { if (reg.IsMatch(line)) { return true; } } rIn.Close(); } return false; Thats a BIG difference.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                A well deserved 5 for answering your own question.

                Deja View - the feeling that you've seen this post before.

                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