I was trying to write one code that needs to fetch the old values stored in some proprietary data base.
To fetch the data, some API and associated functions are provided.The code is as follows.
Now if I run this code, it takes almost 355 seconds. I have measured the time of execution for one point and it took almost 10 seconds(in an avg) to do that.
The test was done with approximately 37 such 'pnt' and it took almost 355 seconds to fetch the required values.The I need to export the same to excel
also.But exporting to excel is not time consuming compared to fetching of data from a time series database through predefined APIs.
I do not have any control in the APIs and I can not do anything on the same.
Considering all this, can I implement parallel processing.I have machine with 24 cores CPU where the program needs to be run.
If somebody can tell me how to implement the same in the code it will be helpful for me.
I was reading some article in this regard, but it seems difficult for me to implement in the code.
If some one cane help me out implementing parallel processing in this code, it will be a great help in reducing time taken by the program.
What I have tried:
int i = 0, j = 0;
string pnt = "";
TimeSpan span;
if (periodtime.SelectedIndex == 0)
{
span = TimeSpan.FromSeconds(Convert.ToInt16(periodval.Text));
}
else if (periodtime.SelectedIndex == 1)
{
span = TimeSpan.FromMinutes(Convert.ToInt16(periodval.Text));
}
else
{
span = TimeSpan.FromHours(Convert.ToInt16(periodval.Text));
}
uint s = 0;
double dval = 0;
DateTime ts = DateTime.Now;
string st = "";
int nRet = 0;
foreach (string slpnt in pntid)
{
pnt = slpnt.Split('-')[0].Split('$')[0];
i = 0;
span = TimeSpan.FromHours(24);
nRet = History.GetHistMin(pnt, Convert.ToDateTime(Fdt.Text + " 00:00:00"), Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24), span, out s);
if (nRet == 0)
{
nRet = History.GetNextHist(s, out dval, out ts, out st);
if (j == 0)
{
dr = dt.NewRow();
dr[0] = "Minimum(of the day)";
if (slpnt.Contains("$"))
{
dr["C" + j.ToString()] = Math.Round(dval, 2);
}
else
{
dr["C" + j.ToString()] = Math.Round(dval, 1);
}
dt.Rows.Add(dr);
}
else
{
if (slpnt.Contains("$"))
{
dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 2);
}
else
{
dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 1);
}
}
}
//==========================================================
nRet = History.GetHistMax(pnt, Convert.ToDateTime(Fdt.Text + " 00:00:00"), Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24), span, out s);
if (nRet == 0)
{
nRet = History.GetNextHist(s, out dval, out ts, out st);
if (j == 0)
{
dr = dt.NewRow();
dr[0] = "Maximum(of the day)";
if (slpnt.Contains("$"))
{
dr["C" + j.ToString()] = Math.Round(dval, 2);
}
else
{
dr["C" + j.ToString()] = Math.Round(dval, 1);
}
dt.Rows.Add(dr);
}
else
{
if (slpnt.Contains("$"))
{
dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 2);
}
else
{
dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 1);
}
}
}
//=========================================================================================================================
nRet = History.GetHistAvg(pnt, Convert.ToDateTime(Fdt.Text + " 00:00:00"), Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24), span, out s);
if (nRet == 0)
{
nRet = History.GetNextHist(s, out dval, out ts, out st);
if (j == 0)
{
dr = dt.NewRow();
dr[0] = "Average(of the day)";
if (slpnt.Contains("$"))
{
dr["C" + j.ToString()] = Math.Round(dval, 2);
}
else
{
dr["C" + j.ToString()] = Math.Round(dval, 1);
}
dt.Rows.Add(dr);
}
else
{
if (slpnt.Contains("$"))
{
dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 2);
}
else
{
dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 1);
}
}
}
j++;
}