-4

I am trying to determine whether a given string is an enumeration value or not. It is rather common that the string should be a decimal value. My question is whether it takes longer to do an Enum.TryParse() on the string or to do a regex check that it is a decimal value or do a Int32.TryParse(). If the string is not a numeric (unsigned) decimal value, it is highly likely, but not 100% guaranteed, that the string is an enum value.

This check is made many many times. I want to get the lowest average cost.

Tevya
  • 836
  • 1
  • 10
  • 23
  • Regex are expensive in CPU time – Yair Nevet May 29 '14 at 17:46
  • 1
    Write some benchmarks and see :-) My initial guess is that checking the first character to see if it's a character will be the fastest method of predicting if the value is likely of an enum. – Cameron May 29 '14 at 17:47
  • 7
    It's not really clear what you mean - `Enum.TryParse` will succeed with numeric values as well. It would really help if you'd give a concrete example, including sample input and expected output. – Jon Skeet May 29 '14 at 17:47
  • 2
    You're going to be better off running benchmarks yourself. No one here knows your dataset, loop nuances, or other unique circumstances. – itsme86 May 29 '14 at 17:47
  • When you compile regex however the performance hit is mostly in the startup. – Mike Cheel May 29 '14 at 17:49
  • What is "many many times?" If you're doing a million of these a second, you might worry about the time taken. Otherwise a simple `if (!int.TryParse(...)) { Enum.TryParse(...) }` is easy to code and will perform well. – Jim Mischel May 29 '14 at 17:55
  • What is the question? Decimal is not Int32. – paparazzo May 29 '14 at 18:06
  • 1
    Strings are for humans, ints and enums are for computers. You'll have to convert a string after you obtained it with I/O or a user interface. At which point perf is a non-issue, obtaining the string is already much more expensive than converting it. Optimizing it won't make the operation noticeably faster. – Hans Passant May 29 '14 at 18:48

2 Answers2

1

You should profile your code to determine if this is actually a problem. If not, just do whatever gives you the most clear, concise (working) code. If it is a problem, this is probably a good way to pick which approach to use:

bool probablyANumber = char.IsDigit(myStr[0]);

This should work well because enums can't start with digits, while numbers usually do (with exceptions like "-1" and " 1 "). Whether this is an improvement or just makes things worse depends on your data set.

It's worth noting that enums can be parsed from numbers, so the behavior of Enum.TryParse may not be what you were expecting.

enum MyEnum
{
    Zero,
    One,
    Two,
    Ten = 10
}

MyEnum e;
Enum.TryParse<MyEnum>("10", out e);
// success; e == MyEnum.Ten

MyEnum e;
Enum.TryParse<MyEnum>("11", out e);
// success; e == (MyEnum)11
Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

The fastest? Put the string values in a HashSet. HashSet.Contains()

paparazzo
  • 44,497
  • 23
  • 105
  • 176