O(n^2) faster than O(n)?











up vote
-4
down vote

favorite












I wrote a small program to test the speed of two separate extension methods I created, detailed in the below screenshot:



public static class Extensions
{
public static List<string> ParseEmails(this string emails, char charsToSplitOn)
{
List<string> list = new List<string>();
foreach (string email in emails)
{
string splitArr = email.Replace(" ", "").Split(charsToSplitOn, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in splitArr)
{
list.Add(item);
}
}
return list;
}

public static string ParseEmails2(this string emails, char charsToSplitOn)
{
string str = string.Empty;
foreach (string item in emails)
{
str += item + ';';
}
return str.Replace(" ", "").Split(charsToSplitOn, StringSplitOptions.RemoveEmptyEntries);
}
}


enter image description here



The Main method initializes a Stopwatch class to track the time each method takes to execute iterations amount of times.



The first method, ParseEmails has a for loop within another for loop, while the second method 'ParseEmails2' has only one for loop.



I would expect that then the second method, ParseEmails2, would be faster, as, from my understanding, this is done in O(n) time, whereas my first method, ParseEmails, is done in O(n^2) time.



If this is true, then shouldn't my results point to ParseEmails2 being the faster of the two methods?










share|improve this question
























  • Code is now posted as code.
    – Delfino
    Nov 19 at 18:41






  • 1




    You don't add timings and your methods have different return types. Comparing apples to oranges etc. You cannot assume library methods (Split, Replace) to be O(1)
    – Henk Holterman
    Nov 19 at 18:41












  • ParseEmails2 creates more string instances
    – Encrypt0r
    Nov 19 at 18:43






  • 1




    ParseEmails is not O(n^2) since n would be the number of emails but the inner loop depends on the number of elements in the split array instead. str += item + ';'; is a very inefficient way of appending to the end of a string. In general though, an O(n) algorithm can be slower than an O(n^2) algorithm depending on the size of the input and the associated constant factors.
    – Lee
    Nov 19 at 18:46










  • Be aware that Big-O notation does not directly equate to time used or memory used, it only relates to the growth of those things. If processing the emails always takes 2 years, it's an O(1) algorithm.
    – Lasse Vågsæther Karlsen
    Nov 19 at 18:55















up vote
-4
down vote

favorite












I wrote a small program to test the speed of two separate extension methods I created, detailed in the below screenshot:



public static class Extensions
{
public static List<string> ParseEmails(this string emails, char charsToSplitOn)
{
List<string> list = new List<string>();
foreach (string email in emails)
{
string splitArr = email.Replace(" ", "").Split(charsToSplitOn, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in splitArr)
{
list.Add(item);
}
}
return list;
}

public static string ParseEmails2(this string emails, char charsToSplitOn)
{
string str = string.Empty;
foreach (string item in emails)
{
str += item + ';';
}
return str.Replace(" ", "").Split(charsToSplitOn, StringSplitOptions.RemoveEmptyEntries);
}
}


enter image description here



The Main method initializes a Stopwatch class to track the time each method takes to execute iterations amount of times.



The first method, ParseEmails has a for loop within another for loop, while the second method 'ParseEmails2' has only one for loop.



I would expect that then the second method, ParseEmails2, would be faster, as, from my understanding, this is done in O(n) time, whereas my first method, ParseEmails, is done in O(n^2) time.



If this is true, then shouldn't my results point to ParseEmails2 being the faster of the two methods?










share|improve this question
























  • Code is now posted as code.
    – Delfino
    Nov 19 at 18:41






  • 1




    You don't add timings and your methods have different return types. Comparing apples to oranges etc. You cannot assume library methods (Split, Replace) to be O(1)
    – Henk Holterman
    Nov 19 at 18:41












  • ParseEmails2 creates more string instances
    – Encrypt0r
    Nov 19 at 18:43






  • 1




    ParseEmails is not O(n^2) since n would be the number of emails but the inner loop depends on the number of elements in the split array instead. str += item + ';'; is a very inefficient way of appending to the end of a string. In general though, an O(n) algorithm can be slower than an O(n^2) algorithm depending on the size of the input and the associated constant factors.
    – Lee
    Nov 19 at 18:46










  • Be aware that Big-O notation does not directly equate to time used or memory used, it only relates to the growth of those things. If processing the emails always takes 2 years, it's an O(1) algorithm.
    – Lasse Vågsæther Karlsen
    Nov 19 at 18:55













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











I wrote a small program to test the speed of two separate extension methods I created, detailed in the below screenshot:



public static class Extensions
{
public static List<string> ParseEmails(this string emails, char charsToSplitOn)
{
List<string> list = new List<string>();
foreach (string email in emails)
{
string splitArr = email.Replace(" ", "").Split(charsToSplitOn, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in splitArr)
{
list.Add(item);
}
}
return list;
}

public static string ParseEmails2(this string emails, char charsToSplitOn)
{
string str = string.Empty;
foreach (string item in emails)
{
str += item + ';';
}
return str.Replace(" ", "").Split(charsToSplitOn, StringSplitOptions.RemoveEmptyEntries);
}
}


enter image description here



The Main method initializes a Stopwatch class to track the time each method takes to execute iterations amount of times.



The first method, ParseEmails has a for loop within another for loop, while the second method 'ParseEmails2' has only one for loop.



I would expect that then the second method, ParseEmails2, would be faster, as, from my understanding, this is done in O(n) time, whereas my first method, ParseEmails, is done in O(n^2) time.



If this is true, then shouldn't my results point to ParseEmails2 being the faster of the two methods?










share|improve this question















I wrote a small program to test the speed of two separate extension methods I created, detailed in the below screenshot:



public static class Extensions
{
public static List<string> ParseEmails(this string emails, char charsToSplitOn)
{
List<string> list = new List<string>();
foreach (string email in emails)
{
string splitArr = email.Replace(" ", "").Split(charsToSplitOn, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in splitArr)
{
list.Add(item);
}
}
return list;
}

public static string ParseEmails2(this string emails, char charsToSplitOn)
{
string str = string.Empty;
foreach (string item in emails)
{
str += item + ';';
}
return str.Replace(" ", "").Split(charsToSplitOn, StringSplitOptions.RemoveEmptyEntries);
}
}


enter image description here



The Main method initializes a Stopwatch class to track the time each method takes to execute iterations amount of times.



The first method, ParseEmails has a for loop within another for loop, while the second method 'ParseEmails2' has only one for loop.



I would expect that then the second method, ParseEmails2, would be faster, as, from my understanding, this is done in O(n) time, whereas my first method, ParseEmails, is done in O(n^2) time.



If this is true, then shouldn't my results point to ParseEmails2 being the faster of the two methods?







c# .net big-o






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 18:41

























asked Nov 19 at 18:38









Delfino

4131622




4131622












  • Code is now posted as code.
    – Delfino
    Nov 19 at 18:41






  • 1




    You don't add timings and your methods have different return types. Comparing apples to oranges etc. You cannot assume library methods (Split, Replace) to be O(1)
    – Henk Holterman
    Nov 19 at 18:41












  • ParseEmails2 creates more string instances
    – Encrypt0r
    Nov 19 at 18:43






  • 1




    ParseEmails is not O(n^2) since n would be the number of emails but the inner loop depends on the number of elements in the split array instead. str += item + ';'; is a very inefficient way of appending to the end of a string. In general though, an O(n) algorithm can be slower than an O(n^2) algorithm depending on the size of the input and the associated constant factors.
    – Lee
    Nov 19 at 18:46










  • Be aware that Big-O notation does not directly equate to time used or memory used, it only relates to the growth of those things. If processing the emails always takes 2 years, it's an O(1) algorithm.
    – Lasse Vågsæther Karlsen
    Nov 19 at 18:55


















  • Code is now posted as code.
    – Delfino
    Nov 19 at 18:41






  • 1




    You don't add timings and your methods have different return types. Comparing apples to oranges etc. You cannot assume library methods (Split, Replace) to be O(1)
    – Henk Holterman
    Nov 19 at 18:41












  • ParseEmails2 creates more string instances
    – Encrypt0r
    Nov 19 at 18:43






  • 1




    ParseEmails is not O(n^2) since n would be the number of emails but the inner loop depends on the number of elements in the split array instead. str += item + ';'; is a very inefficient way of appending to the end of a string. In general though, an O(n) algorithm can be slower than an O(n^2) algorithm depending on the size of the input and the associated constant factors.
    – Lee
    Nov 19 at 18:46










  • Be aware that Big-O notation does not directly equate to time used or memory used, it only relates to the growth of those things. If processing the emails always takes 2 years, it's an O(1) algorithm.
    – Lasse Vågsæther Karlsen
    Nov 19 at 18:55
















Code is now posted as code.
– Delfino
Nov 19 at 18:41




Code is now posted as code.
– Delfino
Nov 19 at 18:41




1




1




You don't add timings and your methods have different return types. Comparing apples to oranges etc. You cannot assume library methods (Split, Replace) to be O(1)
– Henk Holterman
Nov 19 at 18:41






You don't add timings and your methods have different return types. Comparing apples to oranges etc. You cannot assume library methods (Split, Replace) to be O(1)
– Henk Holterman
Nov 19 at 18:41














ParseEmails2 creates more string instances
– Encrypt0r
Nov 19 at 18:43




ParseEmails2 creates more string instances
– Encrypt0r
Nov 19 at 18:43




1




1




ParseEmails is not O(n^2) since n would be the number of emails but the inner loop depends on the number of elements in the split array instead. str += item + ';'; is a very inefficient way of appending to the end of a string. In general though, an O(n) algorithm can be slower than an O(n^2) algorithm depending on the size of the input and the associated constant factors.
– Lee
Nov 19 at 18:46




ParseEmails is not O(n^2) since n would be the number of emails but the inner loop depends on the number of elements in the split array instead. str += item + ';'; is a very inefficient way of appending to the end of a string. In general though, an O(n) algorithm can be slower than an O(n^2) algorithm depending on the size of the input and the associated constant factors.
– Lee
Nov 19 at 18:46












Be aware that Big-O notation does not directly equate to time used or memory used, it only relates to the growth of those things. If processing the emails always takes 2 years, it's an O(1) algorithm.
– Lasse Vågsæther Karlsen
Nov 19 at 18:55




Be aware that Big-O notation does not directly equate to time used or memory used, it only relates to the growth of those things. If processing the emails always takes 2 years, it's an O(1) algorithm.
– Lasse Vågsæther Karlsen
Nov 19 at 18:55












4 Answers
4






active

oldest

votes

















up vote
0
down vote













You have to be precise about what you mean by O(n^2). What is n? If n corresponds to the number of emails (in the emails array), then the first method runs on the scale of O(n), because you iterate over all emails once.



In the second method you use string concatenation, which means that you create a new string each run of the loop resulting in a complexity of O(n^2).



Prefer using StringBuilder or Lists over concatenating strings inside a loop.






share|improve this answer























  • "both methods run on the scale of O(n), because in both methods you iterate once over all emails." That's only true if the body of the loop is an O(1) operation, which it is not.
    – Servy
    Nov 19 at 18:51










  • @Servy The body of the loop is constant in terms of the number of emails, it depends on the size of individual emails. This is where you have to apply common sense and ask yourself whether you have to worry about high variance in size of different emails, if not, you can treat it as a constant.
    – areller
    Nov 19 at 18:53






  • 1




    That statement is true for the first solution. The second solution's loop body's execution time is dependent on the number of emails.
    – Servy
    Nov 19 at 18:55


















up vote
0
down vote













Well...you don't seem to understand the purpose of the O-notation, O is less about the performance and more about the consistancy of performance. O(n²) means a parabola and O(n) a straight line, but it doesn't tell you about the actual time. So once you get to many elements, the O(n) algorithm should be faster. A reason for that could be that you use string's += and + operators which use the copy on write-semantic of C# string.






share|improve this answer




























    up vote
    -1
    down vote













    ParseEmails2 is not actually O(n), because



    str += item + ';';


    contains a loop.






    share|improve this answer





















    • @HenkHolterman Does the compiler optimize away all the re-allocation and copying the ever growing string into new string objects?
      – Timbo
      Nov 19 at 18:46












    • @Timbo it does create a new instance, but it's not a loop. It's equivalent of saying: str = str + item + ';'
      – Encrypt0r
      Nov 19 at 18:47








    • 1




      @Encrypt0r But initializing the new instance with characters does iterate over all the characters of the the parts string is constructed from?
      – Timbo
      Nov 19 at 18:49










    • @Timbo well, in that case, Replace and Split are also loops. I think concatenating two strings in c# should be considered as one operation.
      – Encrypt0r
      Nov 19 at 18:58






    • 1




      @Encrypt0r Yes, replacing, splitting, or concattenating strings are all operations that are O(n) where n is the size of the string(s) the operation is being performed on. The first loop only performs those operations on small strings, the individual strings being looped on (making the loop O(2*n), effectively), the second performs them on very large strings, specifically, the aggregate of all of the strings processed so far, making it O(n*(n -1)/2) or O(n^2),
      – Servy
      Nov 19 at 19:16


















    up vote
    -1
    down vote













    you should check this link. string concatenation vs builder performance
    String concatenation vs String Builder. Performance



    The string builder will most likely be marginally faster in this case, but really it's probably not going to be enough to fret about. The number of times you are re-creating the string (because strings are immutable and joining forces a new string to be created from the two existing ones).



    Because of memory allocation on very step will cause more time.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380764%2fon2-faster-than-on%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      You have to be precise about what you mean by O(n^2). What is n? If n corresponds to the number of emails (in the emails array), then the first method runs on the scale of O(n), because you iterate over all emails once.



      In the second method you use string concatenation, which means that you create a new string each run of the loop resulting in a complexity of O(n^2).



      Prefer using StringBuilder or Lists over concatenating strings inside a loop.






      share|improve this answer























      • "both methods run on the scale of O(n), because in both methods you iterate once over all emails." That's only true if the body of the loop is an O(1) operation, which it is not.
        – Servy
        Nov 19 at 18:51










      • @Servy The body of the loop is constant in terms of the number of emails, it depends on the size of individual emails. This is where you have to apply common sense and ask yourself whether you have to worry about high variance in size of different emails, if not, you can treat it as a constant.
        – areller
        Nov 19 at 18:53






      • 1




        That statement is true for the first solution. The second solution's loop body's execution time is dependent on the number of emails.
        – Servy
        Nov 19 at 18:55















      up vote
      0
      down vote













      You have to be precise about what you mean by O(n^2). What is n? If n corresponds to the number of emails (in the emails array), then the first method runs on the scale of O(n), because you iterate over all emails once.



      In the second method you use string concatenation, which means that you create a new string each run of the loop resulting in a complexity of O(n^2).



      Prefer using StringBuilder or Lists over concatenating strings inside a loop.






      share|improve this answer























      • "both methods run on the scale of O(n), because in both methods you iterate once over all emails." That's only true if the body of the loop is an O(1) operation, which it is not.
        – Servy
        Nov 19 at 18:51










      • @Servy The body of the loop is constant in terms of the number of emails, it depends on the size of individual emails. This is where you have to apply common sense and ask yourself whether you have to worry about high variance in size of different emails, if not, you can treat it as a constant.
        – areller
        Nov 19 at 18:53






      • 1




        That statement is true for the first solution. The second solution's loop body's execution time is dependent on the number of emails.
        – Servy
        Nov 19 at 18:55













      up vote
      0
      down vote










      up vote
      0
      down vote









      You have to be precise about what you mean by O(n^2). What is n? If n corresponds to the number of emails (in the emails array), then the first method runs on the scale of O(n), because you iterate over all emails once.



      In the second method you use string concatenation, which means that you create a new string each run of the loop resulting in a complexity of O(n^2).



      Prefer using StringBuilder or Lists over concatenating strings inside a loop.






      share|improve this answer














      You have to be precise about what you mean by O(n^2). What is n? If n corresponds to the number of emails (in the emails array), then the first method runs on the scale of O(n), because you iterate over all emails once.



      In the second method you use string concatenation, which means that you create a new string each run of the loop resulting in a complexity of O(n^2).



      Prefer using StringBuilder or Lists over concatenating strings inside a loop.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 19 at 19:02

























      answered Nov 19 at 18:48









      areller

      1,49511528




      1,49511528












      • "both methods run on the scale of O(n), because in both methods you iterate once over all emails." That's only true if the body of the loop is an O(1) operation, which it is not.
        – Servy
        Nov 19 at 18:51










      • @Servy The body of the loop is constant in terms of the number of emails, it depends on the size of individual emails. This is where you have to apply common sense and ask yourself whether you have to worry about high variance in size of different emails, if not, you can treat it as a constant.
        – areller
        Nov 19 at 18:53






      • 1




        That statement is true for the first solution. The second solution's loop body's execution time is dependent on the number of emails.
        – Servy
        Nov 19 at 18:55


















      • "both methods run on the scale of O(n), because in both methods you iterate once over all emails." That's only true if the body of the loop is an O(1) operation, which it is not.
        – Servy
        Nov 19 at 18:51










      • @Servy The body of the loop is constant in terms of the number of emails, it depends on the size of individual emails. This is where you have to apply common sense and ask yourself whether you have to worry about high variance in size of different emails, if not, you can treat it as a constant.
        – areller
        Nov 19 at 18:53






      • 1




        That statement is true for the first solution. The second solution's loop body's execution time is dependent on the number of emails.
        – Servy
        Nov 19 at 18:55
















      "both methods run on the scale of O(n), because in both methods you iterate once over all emails." That's only true if the body of the loop is an O(1) operation, which it is not.
      – Servy
      Nov 19 at 18:51




      "both methods run on the scale of O(n), because in both methods you iterate once over all emails." That's only true if the body of the loop is an O(1) operation, which it is not.
      – Servy
      Nov 19 at 18:51












      @Servy The body of the loop is constant in terms of the number of emails, it depends on the size of individual emails. This is where you have to apply common sense and ask yourself whether you have to worry about high variance in size of different emails, if not, you can treat it as a constant.
      – areller
      Nov 19 at 18:53




      @Servy The body of the loop is constant in terms of the number of emails, it depends on the size of individual emails. This is where you have to apply common sense and ask yourself whether you have to worry about high variance in size of different emails, if not, you can treat it as a constant.
      – areller
      Nov 19 at 18:53




      1




      1




      That statement is true for the first solution. The second solution's loop body's execution time is dependent on the number of emails.
      – Servy
      Nov 19 at 18:55




      That statement is true for the first solution. The second solution's loop body's execution time is dependent on the number of emails.
      – Servy
      Nov 19 at 18:55












      up vote
      0
      down vote













      Well...you don't seem to understand the purpose of the O-notation, O is less about the performance and more about the consistancy of performance. O(n²) means a parabola and O(n) a straight line, but it doesn't tell you about the actual time. So once you get to many elements, the O(n) algorithm should be faster. A reason for that could be that you use string's += and + operators which use the copy on write-semantic of C# string.






      share|improve this answer

























        up vote
        0
        down vote













        Well...you don't seem to understand the purpose of the O-notation, O is less about the performance and more about the consistancy of performance. O(n²) means a parabola and O(n) a straight line, but it doesn't tell you about the actual time. So once you get to many elements, the O(n) algorithm should be faster. A reason for that could be that you use string's += and + operators which use the copy on write-semantic of C# string.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          Well...you don't seem to understand the purpose of the O-notation, O is less about the performance and more about the consistancy of performance. O(n²) means a parabola and O(n) a straight line, but it doesn't tell you about the actual time. So once you get to many elements, the O(n) algorithm should be faster. A reason for that could be that you use string's += and + operators which use the copy on write-semantic of C# string.






          share|improve this answer












          Well...you don't seem to understand the purpose of the O-notation, O is less about the performance and more about the consistancy of performance. O(n²) means a parabola and O(n) a straight line, but it doesn't tell you about the actual time. So once you get to many elements, the O(n) algorithm should be faster. A reason for that could be that you use string's += and + operators which use the copy on write-semantic of C# string.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 20:49









          chrissx

          14




          14






















              up vote
              -1
              down vote













              ParseEmails2 is not actually O(n), because



              str += item + ';';


              contains a loop.






              share|improve this answer





















              • @HenkHolterman Does the compiler optimize away all the re-allocation and copying the ever growing string into new string objects?
                – Timbo
                Nov 19 at 18:46












              • @Timbo it does create a new instance, but it's not a loop. It's equivalent of saying: str = str + item + ';'
                – Encrypt0r
                Nov 19 at 18:47








              • 1




                @Encrypt0r But initializing the new instance with characters does iterate over all the characters of the the parts string is constructed from?
                – Timbo
                Nov 19 at 18:49










              • @Timbo well, in that case, Replace and Split are also loops. I think concatenating two strings in c# should be considered as one operation.
                – Encrypt0r
                Nov 19 at 18:58






              • 1




                @Encrypt0r Yes, replacing, splitting, or concattenating strings are all operations that are O(n) where n is the size of the string(s) the operation is being performed on. The first loop only performs those operations on small strings, the individual strings being looped on (making the loop O(2*n), effectively), the second performs them on very large strings, specifically, the aggregate of all of the strings processed so far, making it O(n*(n -1)/2) or O(n^2),
                – Servy
                Nov 19 at 19:16















              up vote
              -1
              down vote













              ParseEmails2 is not actually O(n), because



              str += item + ';';


              contains a loop.






              share|improve this answer





















              • @HenkHolterman Does the compiler optimize away all the re-allocation and copying the ever growing string into new string objects?
                – Timbo
                Nov 19 at 18:46












              • @Timbo it does create a new instance, but it's not a loop. It's equivalent of saying: str = str + item + ';'
                – Encrypt0r
                Nov 19 at 18:47








              • 1




                @Encrypt0r But initializing the new instance with characters does iterate over all the characters of the the parts string is constructed from?
                – Timbo
                Nov 19 at 18:49










              • @Timbo well, in that case, Replace and Split are also loops. I think concatenating two strings in c# should be considered as one operation.
                – Encrypt0r
                Nov 19 at 18:58






              • 1




                @Encrypt0r Yes, replacing, splitting, or concattenating strings are all operations that are O(n) where n is the size of the string(s) the operation is being performed on. The first loop only performs those operations on small strings, the individual strings being looped on (making the loop O(2*n), effectively), the second performs them on very large strings, specifically, the aggregate of all of the strings processed so far, making it O(n*(n -1)/2) or O(n^2),
                – Servy
                Nov 19 at 19:16













              up vote
              -1
              down vote










              up vote
              -1
              down vote









              ParseEmails2 is not actually O(n), because



              str += item + ';';


              contains a loop.






              share|improve this answer












              ParseEmails2 is not actually O(n), because



              str += item + ';';


              contains a loop.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 19 at 18:41









              Timbo

              21.7k103966




              21.7k103966












              • @HenkHolterman Does the compiler optimize away all the re-allocation and copying the ever growing string into new string objects?
                – Timbo
                Nov 19 at 18:46












              • @Timbo it does create a new instance, but it's not a loop. It's equivalent of saying: str = str + item + ';'
                – Encrypt0r
                Nov 19 at 18:47








              • 1




                @Encrypt0r But initializing the new instance with characters does iterate over all the characters of the the parts string is constructed from?
                – Timbo
                Nov 19 at 18:49










              • @Timbo well, in that case, Replace and Split are also loops. I think concatenating two strings in c# should be considered as one operation.
                – Encrypt0r
                Nov 19 at 18:58






              • 1




                @Encrypt0r Yes, replacing, splitting, or concattenating strings are all operations that are O(n) where n is the size of the string(s) the operation is being performed on. The first loop only performs those operations on small strings, the individual strings being looped on (making the loop O(2*n), effectively), the second performs them on very large strings, specifically, the aggregate of all of the strings processed so far, making it O(n*(n -1)/2) or O(n^2),
                – Servy
                Nov 19 at 19:16


















              • @HenkHolterman Does the compiler optimize away all the re-allocation and copying the ever growing string into new string objects?
                – Timbo
                Nov 19 at 18:46












              • @Timbo it does create a new instance, but it's not a loop. It's equivalent of saying: str = str + item + ';'
                – Encrypt0r
                Nov 19 at 18:47








              • 1




                @Encrypt0r But initializing the new instance with characters does iterate over all the characters of the the parts string is constructed from?
                – Timbo
                Nov 19 at 18:49










              • @Timbo well, in that case, Replace and Split are also loops. I think concatenating two strings in c# should be considered as one operation.
                – Encrypt0r
                Nov 19 at 18:58






              • 1




                @Encrypt0r Yes, replacing, splitting, or concattenating strings are all operations that are O(n) where n is the size of the string(s) the operation is being performed on. The first loop only performs those operations on small strings, the individual strings being looped on (making the loop O(2*n), effectively), the second performs them on very large strings, specifically, the aggregate of all of the strings processed so far, making it O(n*(n -1)/2) or O(n^2),
                – Servy
                Nov 19 at 19:16
















              @HenkHolterman Does the compiler optimize away all the re-allocation and copying the ever growing string into new string objects?
              – Timbo
              Nov 19 at 18:46






              @HenkHolterman Does the compiler optimize away all the re-allocation and copying the ever growing string into new string objects?
              – Timbo
              Nov 19 at 18:46














              @Timbo it does create a new instance, but it's not a loop. It's equivalent of saying: str = str + item + ';'
              – Encrypt0r
              Nov 19 at 18:47






              @Timbo it does create a new instance, but it's not a loop. It's equivalent of saying: str = str + item + ';'
              – Encrypt0r
              Nov 19 at 18:47






              1




              1




              @Encrypt0r But initializing the new instance with characters does iterate over all the characters of the the parts string is constructed from?
              – Timbo
              Nov 19 at 18:49




              @Encrypt0r But initializing the new instance with characters does iterate over all the characters of the the parts string is constructed from?
              – Timbo
              Nov 19 at 18:49












              @Timbo well, in that case, Replace and Split are also loops. I think concatenating two strings in c# should be considered as one operation.
              – Encrypt0r
              Nov 19 at 18:58




              @Timbo well, in that case, Replace and Split are also loops. I think concatenating two strings in c# should be considered as one operation.
              – Encrypt0r
              Nov 19 at 18:58




              1




              1




              @Encrypt0r Yes, replacing, splitting, or concattenating strings are all operations that are O(n) where n is the size of the string(s) the operation is being performed on. The first loop only performs those operations on small strings, the individual strings being looped on (making the loop O(2*n), effectively), the second performs them on very large strings, specifically, the aggregate of all of the strings processed so far, making it O(n*(n -1)/2) or O(n^2),
              – Servy
              Nov 19 at 19:16




              @Encrypt0r Yes, replacing, splitting, or concattenating strings are all operations that are O(n) where n is the size of the string(s) the operation is being performed on. The first loop only performs those operations on small strings, the individual strings being looped on (making the loop O(2*n), effectively), the second performs them on very large strings, specifically, the aggregate of all of the strings processed so far, making it O(n*(n -1)/2) or O(n^2),
              – Servy
              Nov 19 at 19:16










              up vote
              -1
              down vote













              you should check this link. string concatenation vs builder performance
              String concatenation vs String Builder. Performance



              The string builder will most likely be marginally faster in this case, but really it's probably not going to be enough to fret about. The number of times you are re-creating the string (because strings are immutable and joining forces a new string to be created from the two existing ones).



              Because of memory allocation on very step will cause more time.






              share|improve this answer

























                up vote
                -1
                down vote













                you should check this link. string concatenation vs builder performance
                String concatenation vs String Builder. Performance



                The string builder will most likely be marginally faster in this case, but really it's probably not going to be enough to fret about. The number of times you are re-creating the string (because strings are immutable and joining forces a new string to be created from the two existing ones).



                Because of memory allocation on very step will cause more time.






                share|improve this answer























                  up vote
                  -1
                  down vote










                  up vote
                  -1
                  down vote









                  you should check this link. string concatenation vs builder performance
                  String concatenation vs String Builder. Performance



                  The string builder will most likely be marginally faster in this case, but really it's probably not going to be enough to fret about. The number of times you are re-creating the string (because strings are immutable and joining forces a new string to be created from the two existing ones).



                  Because of memory allocation on very step will cause more time.






                  share|improve this answer












                  you should check this link. string concatenation vs builder performance
                  String concatenation vs String Builder. Performance



                  The string builder will most likely be marginally faster in this case, but really it's probably not going to be enough to fret about. The number of times you are re-creating the string (because strings are immutable and joining forces a new string to be created from the two existing ones).



                  Because of memory allocation on very step will cause more time.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 18:53









                  Sameer Ahmad

                  14818




                  14818






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380764%2fon2-faster-than-on%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python