与在 VS 2107 调试对话框模式下运行相同代码相比,使用已安装的应用程序发行版时,我的一个函数在同一台计算机上的运行速度要慢 10 倍。很明显,调试模式做了一些事情或预加载了一些有助于代码运行得更快的事情,我不知道它是什么?
我试图让代码更高效,但这无济于事,因为问题与代码的复杂性无关,它实际上很简单。
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();
}