Is it more or less efficient to perform a check before performing a Replace in C#?

Viewed 1574

This is an almost academic question but I'm curious as to its answer.

Suppose you have a loop that performs a routine replace on every row in a dataset. Let's say there's 10,000 such rows.

Is it more efficient to have something like this:

 Row = Row.Replace('X', 'Y');

Or to check whether the row even contains the character that is to be replaced in the first place, like this:

 if (Row.Contains('X')) Row = Row.Replace('X', 'Y');

Is there any difference in terms of efficiency? I realize that that the difference might be very minor bit I'm interested in knowing if one way is better than the other regardless of how much better it may be. Also, would your answer be different if the probability of finding the character that's to be replaced was 10% from it it being 90%?

4 Answers
Related