Usually catching exceptions has a large overhead, and you should catch exceptions if you can do something with them.
In your case, you can do something about the exception. In my opinion, using it as a control flow is not a problem, but I suggest you implement the logic (check different conditions to prevent exceptions), then compare both parameters and compare performance, since usually catching exceptions have high overhead, but if checking an exception exceptions takes longer, then handling the exception is the best way.
Update due to OP comment (its a new implementation, we do not use the Rational .NET framework. Numerator type and long denominator)
you can use larger types to avoid overflow exception like decimal or BigInteger
decimal thisNumerator = this.numerator; decimal thisDenominator = this.numerator; decimal otherNumerator = other.numerator; decimal otherDenominator = other.numerator; checked { return thisNumerator * otherDenominator == thisDenominator * otherNumerator; }
Update due to comments:
A simple example to display overhead with overhead.
const int Iterations = 100000; var sw = new Stopwatch(); var sum1 = 0; sw.Start(); for (int i = 0; i < Iterations; i++) { try { var s = int.Parse("s" + i); sum1 += s; } catch (Exception) { } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Console.WriteLine(sum1); var sw2 = new Stopwatch(); var sum2 = 0; sw2.Start(); for (int i = 0; i < Iterations; i++) { try { int s; if (int.TryParse("s" + i, out s)) sum2 += s; } catch (Exception) { } } sw2.Stop(); Console.WriteLine(sw2.ElapsedMilliseconds); Console.WriteLine(sum2);
result: exception handling is at least 170 times slower
5123
0
thirty
0
source share