Would this have been optimized by a compiler?

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Compiler
Click For Summary

Discussion Overview

The discussion revolves around the optimization of a string substring search function in C#. Participants explore the efficiency of different implementations and whether compilers can optimize certain operations. The scope includes coding interview strategies, performance considerations, and standard library functions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents a function to check if one string is a substring of another and identifies potential performance issues with unnecessary substring calculations.
  • The same participant questions whether the compiler optimizes string comparisons effectively, suggesting that pointer comparisons could be more efficient.
  • Another participant suggests using the built-in Contains method in C#, arguing that it avoids the need for manual substring extraction and comparison.
  • A different participant mentions a function from the standard C library, strstr, as an alternative for finding substrings, implying that similar functionality exists in other programming languages.
  • One participant raises concerns about edge cases, such as handling empty strings and null values, indicating the need for comprehensive error handling in the implementation.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to substring searching and optimization. There is no consensus on whether the compiler optimizes the operations as suggested, and multiple methods are proposed without agreement on a single best solution.

Contextual Notes

Participants note limitations regarding assumptions about string contents, such as handling null values and empty strings, which remain unresolved in the discussion.

SlurrerOfSpeech
Messages
141
Reaction score
11
I had a coding interview question that asked to write a function to determine whether one string is a substring of another. So I wrote

Code:
public bool IsSubtring ( String s1, String s2 )
{
    // returns true or false depending on whether s1 is contained in s2
     for ( m = s1.Length, int i = 0, j = (s2.Length - m + 1); i < j; ++i )
     {
            if ( s1 == s2.Substring(i,m) ) return true;
     }
     return false;
}

I realize that there's a potential performance bug with

Code:
s1 == s2.Substring(i,m)

because the entire substring on the right side is returned whereas string comparison only needs pointers/indices. What I'm saying is, if

Code:
s1[0] != s2[i]

then the substring doesn't need calculated because we already know there's a mismatch. My procedure has redundant operations.This is unless the compiler doesn't optimize in the way I know it could possibly do. I'm also not aware of anything in the C# standard library that would be the equivalent of

Code:
public bool strcmp ( string si, int ia, int ib, string sj, int ja, jb )
{
     // checks whether the strings si and sj are equal in the range of
     // indices [ia, ib) and [ja, jb) respectively
    
     // ...
}

which is what I would need in this scenario.

Does what I'm asking make sense? What are your thoughts?
 
Technology news on Phys.org
Also ... I just realized that the problem didn't specificy whether the empty string is defined as contained in the empty string and what do if the strings are null and etcetera. Gotta have all my bases covered.

Where is Donald Knuth when you need him?
 
You just need to use Contains method in C#.

Code:
public bool IsSubtring ( String s1, String s2 )
{
     return s2.Contains(s1);
}

You are on the right track that you should find the index entry of the pattern in the string and not obtain the substring before comparison. The right hand side expression I think will be calculated first and then == operator will be called. The == operator will again call Equals method to compare each characters available in both the pattern and the acquired substring.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
3K