1

What, if any, would be the performance difference between these?

/// option 1
string result = data.Split('.').Last<string>();


/// option 2
string[] parts = data.Split('.');
string result = data[data.Length-1];

/// option 3
string result = data.Substring(data.LastIndexOf('.')+1);

Assume that data is a string in the format of part1.part2.part3.

Edit

This is really more idle curiosity than anything else. I haven't cracked open reflector to see what's happening inside of LINQ, but I don't see how it could be faster than direct array manipulation. Thanks for the tips.

0

4 Answers 4

3
void Main()
{
    var tester = new testing();
    var runTimeOne = tester.optionOne();
    runTimeOne.Dump();
    var runTimeTwo = tester.optionTwo();
    runTimeTwo.Dump();
    var runTimeThree = tester.optionThree();
    runTimeThree.Dump();
}
public class testing
{
    public String dataString = "part1.part2.part3";
    public int numRuns = 1000000;

    public TimeSpan optionOne()
    {
        var startTime = DateTime.Now;

        for(var i = 0; i < numRuns; i++)
        {
            string result = dataString.Split('.').Last<string>();
        }
        return (DateTime.Now - startTime);
    }

    public TimeSpan optionTwo()
    {
        var startTime = DateTime.Now;

        for(var i = 0; i < numRuns; i++)
        {
            string[] parts = dataString.Split('.');
            string result = parts[parts.Length-1];

        }
        return (DateTime.Now - startTime);
    }
    public TimeSpan optionThree()
    {
        var startTime = DateTime.Now;

        for(var i = 0; i < numRuns; i++)
        {
            string result = dataString.Substring(dataString.LastIndexOf('.')+1);
        }
        return (DateTime.Now - startTime);
    }
}

Result:

00:00:00.9660552

00:00:00.2010115

00:00:00.0360021

Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't have expected the difference between 1 and 2 to be that big. Note that for long strings the difference between 1 and 2 stays about the same, but the difference between 2 and 3 grows.
3

If you really care, measure it.

I would guess option 3) to be a little faster than the other 2.
But that's only a guess.

Comments

2

Options 1 and 2 are identical in terms of performance because the Last extension method checks whether the underlying enmerable has an indexer and if it does it simply returns the last element. As far as option 3 is concerned you will need to measure it with the other two but I suspect there will be a negligible difference.

1 Comment

Your reasoning doesn't support your conclusion. If option 1 does some work (checking for the presence of an indexer) and afterward does option 2, then most likely option 1 is slower than option 2.
1

For small strings the difference would be small, but for large strings the third option is clearly more efficient.

The first two options will scan the entire string and copy it into new strings and create an array of the strings. The third option will just scan the string from the end to look for the period, and just create the one string that is needed.

So, for a specific use you would measure which method is most efficient for the specific data that you have, but for general use the third option is the best because it scales much better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.