C# Code is taking TOO LONG to run, Need to increase the speed of code.

Need to increase speed of code.  The below code works.  The problem is that I am receiving approximately 4,000 records from the Stored Procedure.  The below process is taking a long time to process.  I believe it is occurring at EnumValidation.FindFields.

Any idea how to speed up the process?
public List<T> SelectSPROC(string sprocName, List<Parameters> parameters) 
        {
            List<T> result = new List<T>();

            try
            {
                using (SQLConn = new SqlConnection(ConnectionStrings.GetConnectionString()))
                using (SQLCmd = new SqlCommand())
                {

                    SQLCmd.CommandText = sprocName;
                    SQLCmd.CommandType = CommandType.StoredProcedure;
                    SQLCmd.Connection = SQLConn;
                    SQLCmd.CommandTimeout = 0;

                    if (parameters.Count > 0)
                    {
                        for (int i = 0; i < parameters.Count; i++)
                        {
                            SQLCmd.Parameters.AddWithValue(parameters[i].parameterName, parameters[i].parameterValue);
                        }
                    }

                    SQLConn.Open();

                    SQLRdr = SQLCmd.ExecuteReader();

                    while (SQLRdr.Read())
                    {
                        T t = new T();  

                        for (int inc = 0; inc < SQLRdr.FieldCount; inc++)
                        {
                            Type type = t.GetType();
                            string column = SQLRdr.GetName(inc);
                            //Console.WriteLine(column);
                            if (!EnumValidation.FindFields<Enumerators.ExcludedFields>(column))  //SEEMS TO SLOW DOWN HERE
                            {
                                PropertyInfo prop = type.GetProperty(column);
                                string value = string.Empty;
                                if (ServiceDate.IsDate(SQLRdr.GetValue(inc).ToString()) &&
                                    !SQLRdr.GetValue(inc).ToString().Contains('.') &&
                                    EnumValidation.FindFields<Enumerators.DateFields>(column))
                                {
                                    try
                                    {
                                        DateTime dt = Convert.ToDateTime(SQLRdr.GetValue(inc));
                                        value = dt.ToString("yyyyMMdd");
                                    }
                                    catch (Exception ex)
                                    {
                                        string err = ex.Message;
                                    }
                                }
                                else
                                {
                                    value = SQLRdr.GetValue(inc).ToString();
                                }
                                prop.SetValue(t, value, null);
                            }
                        }

                        result.Add(t);
                        Console.WriteLine(result.Count);
                    }
                    SQLRdr.Close();
                }
            }
            catch (Exception ex)
            {
                filename = AppSettings.GetExportLocationArchive() + AppSettings.GetExportErrorFileName();
                ExportResult.WriteError(ex.ToString(), filename);
                Environment.Exit(1);
            }
            finally
            {
                SQLConn.Close();
                SQLCmd.Dispose();
            }

            return result;
        }

Open in new window

public static class Enumerators
    {
        public enum ExcludedFields
        {
            Status,
            distributorName,
            notes,
            revenueCategory
        }

        public enum DateFields
        {
            dateOrder,
            requiredDate,
            promisedDate,
            shipDate
        }
    }

Open in new window

//This class is where it seems to slow down.
    public static class EnumValidation
    {
        public static bool FindFields<T>(string value)
        {
            try
            {
                if (typeof(T).IsEnum)
                {
                    T column = (T)Enum.Parse(typeof(T), value);
                    if (Enum.IsDefined(typeof(T), column))
                        return true;
                    else
                        return false;
                }
                else
                {
                    throw new ApplicationException("Enum is not valid");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

Open in new window

    public class Parameters
    {
        public string parameterName { get; set; }
        public string parameterValue { get; set; }
    }

Open in new window

LVL 2
CipherISAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aranaCommented:
Enum is really slow as it uses system.reflection, I would try to move your validation out of the loop, calling Enum.GetValues before the import begins, or use if you are using a fast disk reading enum from cache should be faster than calling it on each cycle of your loop. Throwing exceptions (as you do in your validation) has also performance penalties which kind of slows your code a bit more.
You can also create a Hash set and then use CONTAINS inside your loop, you will definately see some performance gain, but if you need even more then you will have to think of something else.
 
At this link you can find an example that you would have to adapt to your needs (not created by me) , it deals with exactly the performance issues you mention.
Enum Validation Performance
Since your validation seems to be customized excluding some fields etc you maybe need to try a different approach but the thread has some alternative Ideas, Sorry I can not be of better help but I think the link covers most of what you need to address.
0
käµfm³d 👽Commented:
Profile your code. Find out where the slowness is rather than try to guess-and-check or suppose where the slowness is.
0
CipherISAuthor Commented:
How do you use CodeTrack to figure out the issue?
codetrack.JPG
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

CipherISAuthor Commented:
Ok
FindFields takes 2 m 36 s
DateTime.TryParse (IsDate) takes 1 m 40 s

Those 2 areas seem to be slowing the app.

Any idea how to increase the speed?
0
CipherISAuthor Commented:
I read this article and I was going to remove the Enums and create a property that would be used to validate the column against the fields in the property which meant using an "if" statement (yuck).

https://volaresystems.com/blog/post/2015/01/09/Stop-wasting-time-with-enums-in-C

After some playing around this is the change

    public static class EnumValidation
    {
        public static bool FindFields<T>(string value)
        {
            try
            {
                if (Enum.IsDefined(typeof(T), value))
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

Open in new window

    public static class ServiceDate
    {
        public static bool IsDate(string value)
        {
            try
            {
                DateTime dt;
                if (DateTime.TryParse(value, out dt))
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
    }

Open in new window

So, xxxx.Parse is what was causing the delay.  The process dropped to under 25 ms.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CipherISAuthor Commented:
Code is the solution to increase the speed.  I was going to selected assisted solution but didn't see that available.  Thanks for providing info to help me resolve the issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.