-1

I know 2 ways to check if the first char in string are some char.

var str = "/checking";
if (str.StartsWith("/"))
    return;

and the over way:

var str = "/checking";
if (str[0] == '/')
    return;

In this case (where I need to check the char, not the substring) whinh way would be faster? And is there any difference?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • 4
    `StartsWith` is more readable and it’s not slow, so you would generally pick it. (And it works when `str == ""`.) If you need the *fastest* one for your case, benchmark them. – Ry- Dec 22 '17 at 22:27
  • Probably the second way, but it's not going to be measurable. In any case, if you care that much, you could always test it. – Blorgbeard Dec 22 '17 at 22:27
  • You might find value in this discussion: https://stackoverflow.com/questions/498686/is-string-contains-faster-than-string-indexof – Brien Foss Dec 22 '17 at 22:28
  • 4
    Also: please read: https://ericlippert.com/2012/12/17/performance-rant/ – Blorgbeard Dec 22 '17 at 22:28
  • 1
    The second is certainly faster, no function call overhead, etc. However, you don't have an is empty check. Validation always complicates 'fast' things. – Thomas Dec 22 '17 at 22:28
  • 2
    Which one is faster: [Measure It](https://github.com/idvorkin/measureitdotnet) – rene Dec 22 '17 at 22:28
  • `if(str.Contains("/"))` you can use that too this is more of an opinionated question by the way.. test all suggested approaches and see which one performs best under your usecase – MethodMan Dec 22 '17 at 22:34
  • It depends, check it yourself, results can be little weird for you :D : http://rextester.com/QWXP17187 To sum it up: StartsWith is optimized, but have same overhead, so for lower tours will be slower. – sTrenat Dec 22 '17 at 22:52
  • @sTrenat bigger difference than I expected. You can use StringComparison.Ordinal to close the gap a bit. – Antonín Lejsek Dec 22 '17 at 23:32
  • Ok, so, thanks everyone for the replies! @MethodMan, I can't use contains - I need to know what is the first char, and contains can return true if the string would be something like "asdasd/gsadfsdf", and that would be false result for me. – Vlad-00003 Dec 23 '17 at 02:49
  • @sTrenat, very interesting. But according to your results - the direct method is a way faster. Thank you for this! – Vlad-00003 Dec 23 '17 at 02:51

1 Answers1

0

The second way will be much faster. The first way is designed to handle a string thus its code needs to be compatible with different comparisons as well. The second way will work only for this condition => will take less time

Source Code: StartsWith

TheSiIence
  • 327
  • 2
  • 10
  • 1
    Do you have any links substantiating you statements? – zx485 Dec 22 '17 at 22:44
  • @zx485 You don't need one, only logic: The first one will try to run on a string (multiple chars) and will contain more conditions and more fail-safes, when the other one will only try to access and won't even try to block the 'crash' in case of no char in index – TheSiIence Dec 22 '17 at 22:45
  • 1
    @Administrator, just as I thought. Anyway - thanks. I just didn't know that there is a such big difference, as sTrenat mention in the comments - rextester.com/QWXP17187 – Vlad-00003 Dec 23 '17 at 02:53
  • @Vlad-00003 no problem, but just mark the answer as valid if it helped you :) – TheSiIence Dec 23 '17 at 10:23