Would this have been optimized by a compiler?

  • Thread starter SlurrerOfSpeech
  • Start date
  • Tags
    Compiler
In summary: Returns a pointer to the first occurrence of needle, or a null pointer if haystack does not contain needle.
  • #1
SlurrerOfSpeech
141
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
  • #2
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?
 
  • #3
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.
 
  • #4

1. Can a compiler optimize my code automatically?

Yes, a compiler can optimize your code automatically by analyzing your code and making changes to improve its performance.

2. How does a compiler optimize my code?

A compiler optimizes your code by removing redundant or unused code, rearranging instructions to improve efficiency, and replacing complex code with simpler alternatives.

3. Will my code run faster if it is optimized by a compiler?

Yes, optimized code will generally run faster than unoptimized code. However, the extent of the improvement will depend on the specific optimizations applied and the complexity of the code.

4. Are there any downsides to using a compiler to optimize my code?

While most optimizations will improve performance, some may also change the behavior of your code. It's important to thoroughly test your code after optimization to ensure that it still functions as intended.

5. Can I control the level of optimization applied by a compiler?

Yes, most compilers offer different levels of optimization that can be specified by the user. These levels may vary in terms of the optimizations applied and the impact on code behavior.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
940
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
Back
Top