7

I wonder if it's a better practice to use :

var a = b.Substring(6);  

Or

var a = b.Remove(0,6);  

Which one is more efficient / faster ? Obviously substring has more options you can pick from but nothing that Remove() cant do.. Sorry if it's newbie question, I'm new to C#

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
dB.Employee
  • 173
  • 3
  • 10
  • 2
    Substring makes more sense in terms of getting a substring from a string heh – austin wernli Dec 09 '15 at 20:07
  • So there's no difference between both ?They do the same work for the same amount of time ? – dB.Employee Dec 09 '15 at 20:07
  • 1
    Actually, i'm pretty sure Substring will return you a new string with the first 6 characters.. remove will return you a string without the first 6 characters... – austin wernli Dec 09 '15 at 20:08
  • Test it out. Read the docs. Don't bother worrying about performance unless you profile it and see that you are spending lots of time in one of the methods. – Derek Dec 09 '15 at 20:08
  • 2
    Use whatever convey the intent clearly. There shouldn't be much difference between these two, even if there is a difference it would be negligible. – Habib Dec 09 '15 at 20:09
  • Efficiency is not a consideration between the two. Their implementation is almost exactly the same. Use the one that conveys the most meaning to you. – Glorin Oakenfoot Dec 09 '15 at 20:09
  • Thanks for the fast and positive feedback guys ! – dB.Employee Dec 09 '15 at 20:10
  • Using ILSpy, `SubString` has a lot more parameter checks than `Remove`. – gmiley Dec 09 '15 at 20:11

3 Answers3

7

Looking at the code using reflector, InternalSubString is doing only one wstrcpy whereas Remove is doing two of them. My guess would be the first one(SubString) is a tad faster.

Here is the code for the Remove method of the string class:

public unsafe string Remove(int startIndex, int count)
{
//...
        string text = string.FastAllocateString(num);

        fixed (char* ptr = &this.m_firstChar)
        {
            fixed (char* ptr2 = &text.m_firstChar)
            {
                string.wstrcpy(ptr2, ptr, startIndex);
                string.wstrcpy(ptr2 + (IntPtr)startIndex, ptr + (IntPtr)startIndex + (IntPtr)count, num - startIndex);
            }
        }
}

And the code called by the SubString method:

private unsafe string InternalSubString(int startIndex, int length)
{
    string text = string.FastAllocateString(length);
    fixed (char* ptr = &text.m_firstChar)
    {
        fixed (char* ptr2 = &this.m_firstChar)
        {
            string.wstrcpy(ptr, ptr2 + (IntPtr)startIndex, length);
        }
    }
    return text;
}
Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
3

Substring is faster based on this post:

Fastest way to remove first char in a String

"I do check now by call each one about 90000000 and I go the following result:

Remove: 06.63 - TrimStart: 04.71 - Substring: 03.09 so from result Substring is the best" - @Amr Badawy

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Aaron
  • 1,361
  • 1
  • 13
  • 30
  • 2
    I wouldn't put too much faith in that benchmark. It may be true, but we don't truly know how that benchmark was performed. – sstan Dec 09 '15 at 20:18
2

String instances are immutable - both Substring() and Remove() will allocate a new string if the return value is different, and will return the same string if it's not, as in this case. Substring better reflect the intent, and should be preferred - almost always, it's better to make the code easily understandable than to worry about minute performance differences.

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Eugene Shvets
  • 4,561
  • 13
  • 19