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. Other Discussions
  3. The Weird and The Wonderful
  4. Yes I know it was me who wrote that...

Yes I know it was me who wrote that...

Scheduled Pinned Locked Moved The Weird and The Wonderful
29 Posts 13 Posters 2 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.
  • Richard DeemingR Richard Deeming

    Are you sure? They both use a virtually identical approach behind the scenes. And since the original code is only comparing the first three characters of the item string, you'd have to use Substring to create a new string before you could call Equals, which would almost certainly be slower.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #13

    Yes!

            string string1 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
            string string2 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
            int x = 0;
            Stopwatch sw1 = new Stopwatch();
            sw1.Start();
            for (int i = 0; i < 1000; i++)
                {
                if (String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase))
                    {
                    x++;
                    }
                }
            sw1.Stop();
            Console.WriteLine(x);
            x = 0;
            Stopwatch sw2 = new Stopwatch();
            sw2.Start();
            for (int i = 0; i < 1000; i++)
                {
                if (String.Compare(string1, string2, true) == 0)
                    {
                    x++;
                    }
                }
            sw2.Stop();
            Console.WriteLine(x);
            Console.WriteLine("{0}:{1}", sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds);
    

    (The file is one of my standard test files: 1.6MB of Ipsum Lorem paragraphs) Results:

    1000
    1000
    1716:4071

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    Richard DeemingR 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      Yes!

              string string1 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
              string string2 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
              int x = 0;
              Stopwatch sw1 = new Stopwatch();
              sw1.Start();
              for (int i = 0; i < 1000; i++)
                  {
                  if (String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase))
                      {
                      x++;
                      }
                  }
              sw1.Stop();
              Console.WriteLine(x);
              x = 0;
              Stopwatch sw2 = new Stopwatch();
              sw2.Start();
              for (int i = 0; i < 1000; i++)
                  {
                  if (String.Compare(string1, string2, true) == 0)
                      {
                      x++;
                      }
                  }
              sw2.Stop();
              Console.WriteLine(x);
              Console.WriteLine("{0}:{1}", sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds);
      

      (The file is one of my standard test files: 1.6MB of Ipsum Lorem paragraphs) Results:

      1000
      1000
      1716:4071

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #14

      That's strange - on my computer, with a similar sized text file, Compare(string, string, bool) is consistently faster:

      1000
      1000
      13997:3431

      I'm running .NET 4.5.1 on Win7 x64. Also, string.Compare(s1, s2, true) isn't the same as string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase); it's equivalent to StringComparison.CurrentCultureIgnoreCase. Try using string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase) instead.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      OriginalGriffO 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        That's strange - on my computer, with a similar sized text file, Compare(string, string, bool) is consistently faster:

        1000
        1000
        13997:3431

        I'm running .NET 4.5.1 on Win7 x64. Also, string.Compare(s1, s2, true) isn't the same as string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase); it's equivalent to StringComparison.CurrentCultureIgnoreCase. Try using string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase) instead.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #15

        Interesting: I'm also Win7/64, but .NET 4.0 rather than 4.5. Adding OrdinalIgnoreCase:

                string string1 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                string string2 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                int x = 0;
                Stopwatch sw1 = new Stopwatch();
                sw1.Start();
                for (int i = 0; i < 1000; i++)
                    {
                    if (String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase))
                        {
                        x++;
                        }
                    }
                sw1.Stop();
                Console.WriteLine(x);
                x = 0;
                Stopwatch sw2 = new Stopwatch();
                sw2.Start();
                for (int i = 0; i < 1000; i++)
                    {
                    if (String.Compare(string1, string2, true) == 0)
                        {
                        x++;
                        }
                    }
                sw2.Stop();
                Console.WriteLine(x);
                x = 0;
                Stopwatch sw3 = new Stopwatch();
                sw3.Start();
                for (int i = 0; i < 1000; i++)
                    {
                    if (String.Compare(string1, string2, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                        x++;
                        }
                    }
                sw3.Stop();
                Console.WriteLine(x);
                Console.WriteLine("{0}:{1}:{2}", sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds,sw3.ElapsedMilliseconds);
        

        gives results similar to yours:

        1000
        1000
        1000
        1694:4087:1684

        I'm surprised there is such a difference in performance between our machines: mine isn't anywhere near state of the art - more state of the ark! :laugh:

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        A Richard DeemingR J L 4 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          Interesting: I'm also Win7/64, but .NET 4.0 rather than 4.5. Adding OrdinalIgnoreCase:

                  string string1 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                  string string2 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                  int x = 0;
                  Stopwatch sw1 = new Stopwatch();
                  sw1.Start();
                  for (int i = 0; i < 1000; i++)
                      {
                      if (String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase))
                          {
                          x++;
                          }
                      }
                  sw1.Stop();
                  Console.WriteLine(x);
                  x = 0;
                  Stopwatch sw2 = new Stopwatch();
                  sw2.Start();
                  for (int i = 0; i < 1000; i++)
                      {
                      if (String.Compare(string1, string2, true) == 0)
                          {
                          x++;
                          }
                      }
                  sw2.Stop();
                  Console.WriteLine(x);
                  x = 0;
                  Stopwatch sw3 = new Stopwatch();
                  sw3.Start();
                  for (int i = 0; i < 1000; i++)
                      {
                      if (String.Compare(string1, string2, StringComparison.OrdinalIgnoreCase) == 0)
                          {
                          x++;
                          }
                      }
                  sw3.Stop();
                  Console.WriteLine(x);
                  Console.WriteLine("{0}:{1}:{2}", sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds,sw3.ElapsedMilliseconds);
          

          gives results similar to yours:

          1000
          1000
          1000
          1694:4087:1684

          I'm surprised there is such a difference in performance between our machines: mine isn't anywhere near state of the art - more state of the ark! :laugh:

          A Offline
          A Offline
          Argonia
          wrote on last edited by
          #16

          Ah, the mysteries of M$. Our life will be so empty and boring without them. The same code (Sorry Griff i should pay you author rights) the result was

          1000
          1000
          1000
          4359:2216:4383

          .Net 4.5 Win 7/64

          Microsoft ... the only place where VARIANT_TRUE != true

          OriginalGriffO 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Interesting: I'm also Win7/64, but .NET 4.0 rather than 4.5. Adding OrdinalIgnoreCase:

                    string string1 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                    string string2 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                    int x = 0;
                    Stopwatch sw1 = new Stopwatch();
                    sw1.Start();
                    for (int i = 0; i < 1000; i++)
                        {
                        if (String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase))
                            {
                            x++;
                            }
                        }
                    sw1.Stop();
                    Console.WriteLine(x);
                    x = 0;
                    Stopwatch sw2 = new Stopwatch();
                    sw2.Start();
                    for (int i = 0; i < 1000; i++)
                        {
                        if (String.Compare(string1, string2, true) == 0)
                            {
                            x++;
                            }
                        }
                    sw2.Stop();
                    Console.WriteLine(x);
                    x = 0;
                    Stopwatch sw3 = new Stopwatch();
                    sw3.Start();
                    for (int i = 0; i < 1000; i++)
                        {
                        if (String.Compare(string1, string2, StringComparison.OrdinalIgnoreCase) == 0)
                            {
                            x++;
                            }
                        }
                    sw3.Stop();
                    Console.WriteLine(x);
                    Console.WriteLine("{0}:{1}:{2}", sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds,sw3.ElapsedMilliseconds);
            

            gives results similar to yours:

            1000
            1000
            1000
            1694:4087:1684

            I'm surprised there is such a difference in performance between our machines: mine isn't anywhere near state of the art - more state of the ark! :laugh:

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #17

            It must be .NET 4.5 - my machine's an early Vista-era dual-core.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • A Argonia

              Ah, the mysteries of M$. Our life will be so empty and boring without them. The same code (Sorry Griff i should pay you author rights) the result was

              1000
              1000
              1000
              4359:2216:4383

              .Net 4.5 Win 7/64

              Microsoft ... the only place where VARIANT_TRUE != true

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #18

              Out of interest, are you building for "Any CPU", "x64" or "x32"? Mine is built "x32" because that's what the app I shoved the code in is built for.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              A 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Out of interest, are you building for "Any CPU", "x64" or "x32"? Mine is built "x32" because that's what the app I shoved the code in is built for.

                A Offline
                A Offline
                Argonia
                wrote on last edited by
                #19

                The results i pasted was with "Any CPU" x64

                1000
                1000
                1000
                5363:2199:5338

                x86

                1000
                1000
                1000
                4530:2305:4519

                Visual studio Premium 2012 version 11 with Update 3, Win7/64, .NET 4.5.50709 to be exact Anyway i don't see how he gets ~14 seconds for String.Equals P.S i should say i have problems with my hard at work. I am waiting for it to die. This also can affect the pasted data. I wonder what the results will be with reading from SSD Note to myself : seconds comes after milliseconds not minutes. Stupid

                Microsoft ... the only place where VARIANT_TRUE != true

                Richard DeemingR 1 Reply Last reply
                0
                • A Argonia

                  The results i pasted was with "Any CPU" x64

                  1000
                  1000
                  1000
                  5363:2199:5338

                  x86

                  1000
                  1000
                  1000
                  4530:2305:4519

                  Visual studio Premium 2012 version 11 with Update 3, Win7/64, .NET 4.5.50709 to be exact Anyway i don't see how he gets ~14 seconds for String.Equals P.S i should say i have problems with my hard at work. I am waiting for it to die. This also can affect the pasted data. I wonder what the results will be with reading from SSD Note to myself : seconds comes after milliseconds not minutes. Stupid

                  Microsoft ... the only place where VARIANT_TRUE != true

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #20

                  It's milliseconds, so it was just under 14 seconds, not minutes! ;P I'm running the code in LinqPad[^], and I've tried both with and without optimisations enabled, but it doesn't make a huge difference.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  A 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    It's milliseconds, so it was just under 14 seconds, not minutes! ;P I'm running the code in LinqPad[^], and I've tried both with and without optimisations enabled, but it doesn't make a huge difference.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    A Offline
                    A Offline
                    Argonia
                    wrote on last edited by
                    #21

                    Yeah i know its in milliseconds but my upper processor forgot that after miliseconds seconds are next not minutes :D I am running it in debug mode with no optimisations.

                    Microsoft ... the only place where VARIANT_TRUE != true

                    R 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Interesting: I'm also Win7/64, but .NET 4.0 rather than 4.5. Adding OrdinalIgnoreCase:

                              string string1 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                              string string2 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                              int x = 0;
                              Stopwatch sw1 = new Stopwatch();
                              sw1.Start();
                              for (int i = 0; i < 1000; i++)
                                  {
                                  if (String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase))
                                      {
                                      x++;
                                      }
                                  }
                              sw1.Stop();
                              Console.WriteLine(x);
                              x = 0;
                              Stopwatch sw2 = new Stopwatch();
                              sw2.Start();
                              for (int i = 0; i < 1000; i++)
                                  {
                                  if (String.Compare(string1, string2, true) == 0)
                                      {
                                      x++;
                                      }
                                  }
                              sw2.Stop();
                              Console.WriteLine(x);
                              x = 0;
                              Stopwatch sw3 = new Stopwatch();
                              sw3.Start();
                              for (int i = 0; i < 1000; i++)
                                  {
                                  if (String.Compare(string1, string2, StringComparison.OrdinalIgnoreCase) == 0)
                                      {
                                      x++;
                                      }
                                  }
                              sw3.Stop();
                              Console.WriteLine(x);
                              Console.WriteLine("{0}:{1}:{2}", sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds,sw3.ElapsedMilliseconds);
                      

                      gives results similar to yours:

                      1000
                      1000
                      1000
                      1694:4087:1684

                      I'm surprised there is such a difference in performance between our machines: mine isn't anywhere near state of the art - more state of the ark! :laugh:

                      J Offline
                      J Offline
                      Jorgen Andersson
                      wrote on last edited by
                      #22

                      You'll have to redo the test using the same textfile. I believe that's where most of the difference lies.

                      Politicians are always realistically manoeuvering for the next election. They are obsolete as fundamental problem-solvers. Buckminster Fuller

                      1 Reply Last reply
                      0
                      • G GuyThiebaut

                        if (item.Substring(0, 3).ToLower() != "PF_")

                        [Head hangs in shame] :doh: I should have coded:

                        if (item.Substring(0, 3).ToLower() != "PF_".ToLower())

                        “That which can be asserted without evidence, can be dismissed without evidence.”

                        ― Christopher Hitchens

                        P Offline
                        P Offline
                        phil o
                        wrote on last edited by
                        #23

                        I would also have added a couple of .ToString()s somewhere, just to be sure :)

                        Women are composed of carbon, hydrogen, oxygen and nitrogen; men are also composed of carbon, hydrogen, oxygen and nitrogen, but in such proportions that force respect.

                        1 Reply Last reply
                        0
                        • G GuyThiebaut

                          if (item.Substring(0, 3).ToLower() != "PF_")

                          [Head hangs in shame] :doh: I should have coded:

                          if (item.Substring(0, 3).ToLower() != "PF_".ToLower())

                          “That which can be asserted without evidence, can be dismissed without evidence.”

                          ― Christopher Hitchens

                          N Offline
                          N Offline
                          Nagy Vilmos
                          wrote on last edited by
                          #24

                          You really didn't think it through:

                          char p = item.charAt[0];
                          char f = item.charAt[1]
                          char u = item.charAt[1]
                          if (p == 'P' || p == 'p' &&
                          u == '_' &&
                          f == 'f' || p == 'F')
                          {
                          // you're good to go....
                          }

                          speramus in juniperus

                          Richard DeemingR 1 Reply Last reply
                          0
                          • N Nagy Vilmos

                            You really didn't think it through:

                            char p = item.charAt[0];
                            char f = item.charAt[1]
                            char u = item.charAt[1]
                            if (p == 'P' || p == 'p' &&
                            u == '_' &&
                            f == 'f' || p == 'F')
                            {
                            // you're good to go....
                            }

                            speramus in juniperus

                            Richard DeemingR Offline
                            Richard DeemingR Offline
                            Richard Deeming
                            wrote on last edited by
                            #25

                            Nagy Vilmos wrote:

                            f == 'f' || p == 'F'

                            Well that's just cruel! If you're going to rewrite it, at least make sure it works. :doh:


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                            N 1 Reply Last reply
                            0
                            • A Argonia

                              Yeah i know its in milliseconds but my upper processor forgot that after miliseconds seconds are next not minutes :D I am running it in debug mode with no optimisations.

                              Microsoft ... the only place where VARIANT_TRUE != true

                              R Offline
                              R Offline
                              Rob Grainger
                              wrote on last edited by
                              #26

                              "I am running it in debug mode with no optimisations." Why on earth try to benchmark code with no optimisations? Complete waste of time.

                              "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                              1 Reply Last reply
                              0
                              • Richard DeemingR Richard Deeming

                                Nagy Vilmos wrote:

                                f == 'f' || p == 'F'

                                Well that's just cruel! If you're going to rewrite it, at least make sure it works. :doh:


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                N Offline
                                N Offline
                                Nicholas Marty
                                wrote on last edited by
                                #27

                                how about using the same character twice and the missing semicolons? :D

                                1 Reply Last reply
                                0
                                • OriginalGriffO OriginalGriff

                                  Interesting: I'm also Win7/64, but .NET 4.0 rather than 4.5. Adding OrdinalIgnoreCase:

                                          string string1 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                                          string string2 = File.ReadAllText(@"D:\\Temp\\MyText.txt");
                                          int x = 0;
                                          Stopwatch sw1 = new Stopwatch();
                                          sw1.Start();
                                          for (int i = 0; i < 1000; i++)
                                              {
                                              if (String.Equals(string1, string2, StringComparison.OrdinalIgnoreCase))
                                                  {
                                                  x++;
                                                  }
                                              }
                                          sw1.Stop();
                                          Console.WriteLine(x);
                                          x = 0;
                                          Stopwatch sw2 = new Stopwatch();
                                          sw2.Start();
                                          for (int i = 0; i < 1000; i++)
                                              {
                                              if (String.Compare(string1, string2, true) == 0)
                                                  {
                                                  x++;
                                                  }
                                              }
                                          sw2.Stop();
                                          Console.WriteLine(x);
                                          x = 0;
                                          Stopwatch sw3 = new Stopwatch();
                                          sw3.Start();
                                          for (int i = 0; i < 1000; i++)
                                              {
                                              if (String.Compare(string1, string2, StringComparison.OrdinalIgnoreCase) == 0)
                                                  {
                                                  x++;
                                                  }
                                              }
                                          sw3.Stop();
                                          Console.WriteLine(x);
                                          Console.WriteLine("{0}:{1}:{2}", sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds,sw3.ElapsedMilliseconds);
                                  

                                  gives results similar to yours:

                                  1000
                                  1000
                                  1000
                                  1694:4087:1684

                                  I'm surprised there is such a difference in performance between our machines: mine isn't anywhere near state of the art - more state of the ark! :laugh:

                                  L Offline
                                  L Offline
                                  ledtech3
                                  wrote on last edited by
                                  #28

                                  Could the difference be a extra file system filter driver slowing one down ?

                                  1 Reply Last reply
                                  0
                                  • Richard DeemingR Richard Deeming

                                    GuyThiebaut wrote:

                                    I should have coded:

                                    if (item.Substring(0, 3).ToLower() != "PF_".ToLower())

                                    I hope that's a good example of sarcasm! ;P

                                    if (string.Compare(item, 0, "PF_", 0, 3, StringComparison.OrdinalIgnoreCase) != 0)


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    V Offline
                                    V Offline
                                    Vladimir Svyatski
                                    wrote on last edited by
                                    #29

                                    I've been always wondering why people just don't use string comparison functions with the case ignoring ability. Many languages have them. Why use ToLower or ToUpper if you don't really need that lowered string? One my colleague once told me that because of the belovers of ToUpper/ToLower the program he had to improve was doing its job during about 11 hours (the program was in C++)!!! After just replacing the appropriate functions with stricmp and similar ones (which compare ignoring the case) the program got the awesome performance boost: it managed to finish its execution in just one hour or such. Taste the difference!

                                    lifecycle of a lifecycle of a lifecycle

                                    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