Debug version of the code runs 10 times faster compared to released installed app

82 Views Asked by At

One of my functions run 10 times slower on the same computer when using the installed release version of the application compared to running the same code in the VS 2107 debug dialog mode. clearly the debug mode does something or pre-load something that helps the code to run faster, I can't figure out what is it ?

I tried to make the code more efficient but it doesn't help because the problem isn't related to the complexity of the code, it is actually simple.

 public void AcuSyncRegularProtocol(string DateStamp, string EnergyStart, string EnergyEnd, string tanentTable)
    {

        List<string> EnergyList = new List<string>();
        List<string> TableList = new List<string>();

        decimal EnergyStartValue = Convert.ToDecimal(EnergyStart);
        decimal EnergyEndValue = Convert.ToDecimal(EnergyEnd);
        decimal EnergyConsumed = EnergyEndValue - EnergyStartValue;
        decimal sharp = 0;
        decimal peack = 0;
        decimal valley = 0;

        string tou_type = "0";
        string tou_tariff = "0";
        string tou_schedule = "0";
        string tou_schedule_type = "0";
        string tou_schedule_number = "0";
        string time = "0";

        int index = -1;

        ConvertStringTime cst = new ConvertStringTime();
        DateTime DateInput = cst.ParseExactConverter(DateStamp);
        if (DateInput.Minute.ToString() == "0") { DateInput = DateInput.AddHours(-1); }

        //Get the correct TOU tables based on the date

        try
        {
            MySqlCommand CycleTableInfo = new MySqlCommand("SELECT table_name FROM information_schema.tables WHERE table_schema ='acumanager';", myConn);

            MySqlDataReader CycleTableInfoReader;
            myConn.Open();
            CycleTableInfoReader = CycleTableInfo.ExecuteReader();
            while (CycleTableInfoReader.Read())
            {
                string tmp = CycleTableInfoReader.GetString("TABLE_NAME");

                //Get only TOU related tables

                if ((tmp.Contains("tou_") == true))
                {
                    DateTime StartTuoDate = cst.ParseExactConverter(tmp);

                    if (StartTuoDate <= DateInput)
                    {
                        TableList.Add(CycleTableInfoReader["TABLE_NAME"].ToString());
                    }
                }
            }
            myConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        //filter old unwanted tables

        for (int i = 0; i < TableList.Count; i++)
        {
            DateTime CurrentTable = cst.ParseExactConverter(TableList[i]);

            for (int j = 0; j < TableList.Count; j++)
            {
                DateTime ReferenceTable = cst.ParseExactConverter(TableList[j]);

                if (ReferenceTable > CurrentTable) { TableList.RemoveAt(i); break; }

            }
        }

        //find the index of the seasons table

        for (int i = 0; i < TableList.Count; i++) { if (TableList[i].Contains("tou_seasons")) { index = i; break; } }

        //Get the correct schedule type based on the day of week

        tou_schedule_type = DateInput.DayOfWeek.ToString();
        if (tou_schedule_type == "Friday") { tou_schedule_type = "schedule-f"; }
        else if (tou_schedule_type == "Saturday") { tou_schedule_type = "schedule-s"; }
        else { tou_schedule_type = "schedule-w"; }

        //Get the schedule number based on schedule type and inputdate month

        try
        {
            MySqlCommand CycleTableInfo = new MySqlCommand("SELECT * FROM acumanager.`" + TableList[index] + "` WHERE month ='" + DateInput.Month.ToString() + "'; ", myConn);

            MySqlDataReader CycleTableInfoReader;
            myConn.Open();
            CycleTableInfoReader = CycleTableInfo.ExecuteReader();
            while (CycleTableInfoReader.Read())
            {
                tou_schedule_number = CycleTableInfoReader.GetString(tou_schedule_type);
            }
            myConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        tou_schedule = "schedule" + tou_schedule_number;
        tou_tariff = "tariff" + tou_schedule_number;

        //find the index of the schedule table

        for (int i = 0; i < TableList.Count; i++) { if (TableList[i].Contains("tou_schedules")) { index = i; break; } }

        //Get the tariff based on schedule number and time

        time = DateInput.Hour.ToString() + ":00" + ":00";

        try
        {
            MySqlCommand CycleTableInfo = new MySqlCommand("SELECT * FROM acumanager.`" + TableList[index] + "` WHERE " + tou_schedule + " ='" + time + "'; ", myConn);
            MySqlDataReader CycleTableInfoReader;
            myConn.Open();
            CycleTableInfoReader = CycleTableInfo.ExecuteReader();

            while (CycleTableInfoReader.Read())
            {
                tou_tariff = CycleTableInfoReader.GetString(tou_tariff);
            }
            myConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        //find the index of the price table

        for (int i = 0; i < TableList.Count; i++) { if (TableList[i].Contains("tou_prices")) { index = i; break; } }

        //Get the correct ennergy type base on the tariff

        try
        {
            MySqlCommand CycleTableInfo = new MySqlCommand("SELECT * FROM acumanager.`" + TableList[index] + "` WHERE tou_price_id ='" + tou_tariff + "'; ", myConn);

            MySqlDataReader CycleTableInfoReader;
            myConn.Open();
            CycleTableInfoReader = CycleTableInfo.ExecuteReader();

            while (CycleTableInfoReader.Read())
            {
                tou_type = CycleTableInfoReader.GetString("nameENG");
            }
            myConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        //Get the previous values of TOU

        try
        {
            MySqlCommand InsertEntry = new MySqlCommand("SELECT * FROM customers." + tanentTable + " ORDER BY Date DESC LIMIT 1;", myConn);
            MySqlDataReader CycleTableInfoReader;
            myConn.Open();
            CycleTableInfoReader = InsertEntry.ExecuteReader();
            while (CycleTableInfoReader.Read())
            {
                sharp = CycleTableInfoReader.GetDecimal("Sharp");
                peack = CycleTableInfoReader.GetDecimal("Peack");
                valley = CycleTableInfoReader.GetDecimal("Valley");
            }
            myConn.Close();
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); myConn.Close(); }
        myConn.Close();

        //add the delta to the correct TOU type

        if (tou_type == "Sharp") { sharp = sharp + EnergyConsumed; }
        else if (tou_type == "Peack") { peack = peack + EnergyConsumed; }
        else if (tou_type == "Valley") { valley = valley + EnergyConsumed; }
        else { MessageBox.Show("תקלה בזיהוי התעריף"); }

        //insert the TOU values back

        try
        {
            MySqlCommand InsertEntry = new MySqlCommand("insert into customers." + tanentTable + " (Date,Energy,Sharp,Peack,Valley) values('" + DateStamp + "','" + EnergyEnd + "','" + sharp + "', '" + peack + "', '" + valley + "')  ;", myConn);
            MySqlDataReader CycleTableInfoReader;
            myConn.Open();
            CycleTableInfoReader = InsertEntry.ExecuteReader();
            myConn.Close();
        }
        catch (Exception ex)
        {
            if (ex.ToString().Contains("Duplicate entry") == false)
            {
                MessageBox.Show(ex.Message);
                myConn.Close();
            }
        }
        myConn.Close();

    }
0

There are 0 best solutions below