Consider the following function:
public enum Operator
{
EQUAL = 1,
GREATER_THAN = 2
}
public class checkString
{
public static bool isValid(string inputString, string checkString, Operator operation)
{
switch (operation)
{
case Operator.EQUAL:
if (inputString == checkString)
return true;
break;
case Operator.GREATER_THAN:
// Numeric check for greater than
try
{
double inputDouble, checkDouble;
inputDouble = Convert.ToDouble(inputString);
checkDouble = Convert.ToDouble(checkString);
if (inputDouble > checkDouble)
return true;
}
catch (Exception)
{ }
// Date check for greater than
try
{
DateTime inputDate, checkDate;
inputDate = DateTime.Parse(inputString);
checkDate = DateTime.Parse(inputString);
if (inputDate. > checkDate)
return true;
}
catch (Exception)
{ }
break;
}
return false;
}
}
Parameters
- inputString: What we want to evaluate
- checkString: The criteria (value) that the input must evaluate against
- Operator: Enum for the operation we want to perform
Other things to know
- Each line in a file is evaluated against this method to return if a condition has been met
- The process of evaluating the records in the file checks line by line, in one instance that its equal to the condition. It may also check that the same line is also greater than the condition. Once the checks are done, it moves to the next record
- There are no additional event listeners hooked up other than whatever defaults are in place, I am not pushing extra data to debug or trace logs
Problem
What people are going to evaluate is unknown to me at any point in this process but I need to be able to check that ‘something’ (regardless of what) is equal to, greater than or less than something else. Sure I check other things but I’ve simplified this function greatly.
That said, using EQUAL or NOT_EQUAL runs fast as can be, processing records in a very large file against said criteria pretty quick and efficiently. Once I added the GREATER_THAN logic, its slow… to the point where it takes minutes to process 20 meg files that used to take half a minute tops.
From what I can tell:
- Exceptions are thrown all over the place. There is no guarantee that a field is going to be numeric or of date type. So I must attempt to cast to these data types to attempt to evaluate the condition
- When exceptions are thrown, the console gets output where I have not instructed it to do so, its sort of automated
Yes I have a lack of experience in this area and am looking to learn more about exception handling and what really happens behind the scenes because when the other 80% of the records are not numeric, thats a lot of exceptions in a 20 meg, 80 thousand record file.
Is there a better way to handle the cast itself to increase efficiency? I’ve seen double.Parse / TryParse and can direct cast in front but am not sure which benefits the most.
You can use TryParse() on those data types. Exceptions are messy and expensive. TryParse will return true/false if it worked or not while NOT throwing an exception. So you can just check the results of the call. Much more efficient than exceptions.
Convert.ToDouble() and Double.Parse() will throw exceptions.
try this code. It isn’t the best, but it’s better than what you have now considering you don’t know what the type could be: