Одна из моих функций работает в 10 раз медленнее на одном и том же компьютере при использовании установленной версии приложения по сравнению с выполнением того же кода в режиме диалога отладки VS 2107.Очевидно, что режим отладки делает что-то или предварительно загружает что-то, что помогает коду работать быстрее, я не могу понять, что это такое?
Я пытался сделать код более эффективным, но это не помогает, потому чтопроблема не связана со сложностью кода, она на самом деле проста.
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();
}