How to restructure my below routine to speed up execution

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
below code is main routine which taking time. basically i am iterating in data table and query datatable by LINQ and fetching data. this function is ExtractStandardValueFromChildRow recursive and it iterate in datatable untill get the value from child element.

can you please tell me where is the glitch in my code which increasing the execution time and also tell me how to restructure my below code to speed up the execution.

if i use AsParallel when query data table then speed will be increase ?

my pc has 4 core and i want to use all the core. so how to restructure the below code. looking for guide line. thanks

my code sample

string _StandardValue = "";
if (dtMappedSectionNotEmpty.AsEnumerable().Any(x => !string.IsNullOrEmpty(x.Field<string>("LinkedItemList"))))
{
//filtering & sort data table for next for iteration
dtFilterForLink = dtMappedSectionNotEmpty.AsEnumerable().Where(x => !string.IsNullOrEmpty(x.Field<string>("LinkedItemList")))
.OrderByDescending(a => a.Field<string>("Matched Section"))
.ThenBy(a => a.Field<string>("Matched Items"))
.ThenByDescending(a => a.Field<string>("Link"))
.CopyToDataTable();

//iterate in datatable of dtFilterForLink where LinkedItemList is not empty
if (dtFilterForLink != null && dtFilterForLink.Rows.Count > 0)
{
for (int y = 0; y <= dtFilterForLink.Rows.Count - 1; y++)
{
//Accumulating data in for iteration
TabName = dtFilterForLink.Rows[y]["Matched Section"].ToString() ?? "";
StandardLineItem = dtFilterForLink.Rows[y]["Matched Items"].ToString() ?? "";
strLink = dtFilterForLink.Rows[y]["Link"].ToString().ToUpper() ?? "";
BRTab = dtFilterForLink.Rows[y]["Tab"].ToString().ToUpper() ?? "";
BRLineItem = (dtFilterForLink.Rows[y]["Formula"] == DBNull.Value ? "" : dtFilterForLink.Rows[y]["Formula"].ToString());
RowNumber = dtFilterForLink.Rows[y]["Row"].ToString().ToUpper() ?? "";
strUnits = (dtFilterForLink.Rows[y]["Units"] == DBNull.Value ? "" : dtFilterForLink.Rows[y]["Units"].ToString());
strAction = dtFilterForLink.Rows[y]["Action"].ToString() ?? "";
cumulative = (dtFilterForLink.Rows[y]["cumulative"] == DBNull.Value ? false : dtFilterForLink.Rows[y]["cumulative"].ToString().ToUpper() == "TRUE" ? true : false);

if (strLink == "TRUE")
{
if (!lstDataCheck.Contains(TabName + "~" + StandardLineItem))
{
lstDataCheck.Add(TabName + "~" + StandardLineItem);

for (int x = 0; x <= _Periods.Count - 1; x++)
{
//query a data table to find out Standard Value based on BRTab, RowNumber & StandardDate
strPeriod = _Periods[x];

StandardValue = string.Empty;

if (dtFilterDataFromAllData.AsEnumerable().Any(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", "")))
{
StandardValue = dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.Select(v => v.Field<string>("StandardValue"))
.FirstOrDefault();
}

if (StandardValue != null && StandardValue.ToString() != "")
{
//update Standard Value after processing
StandardValue = ProcessValue(StandardValue ?? "", strUnits, false);
dtFilterDataFromAllData.AcceptChanges();
}
else
{
if (dtFilterForLink.AsEnumerable().Any(s => s.Field<string>("Matched Section") == TabName
&& s.Field<string>("Matched Items") == StandardLineItem
&& s.Field<string>("Link") == "False"))
{
//accumulating only Link = false data based on Matched Section & Matched Items
DataTable tmpdtFilterForLink = dtFilterForLink.AsEnumerable()
.Where(s => s.Field<string>("Matched Section") == TabName
&& s.Field<string>("Matched Items") == StandardLineItem
&& s.Field<string>("Link") == "False").CopyToDataTable();

//call recursive function ExtractStandardValueFromChildRow to extract Standard Value from next sebsequent rows
ExtractStandardValueFromChildRow(tmpdtFilterForLink,
dtFilterDataFromAllData,
TabName,
StandardLineItem,
BRTab,
RowNumber,
strPeriod,
0,
ref _StandardValue);

if (_StandardValue != "")
{
if (StandardValue == null) //when Standard Value not found
{
//here inserting new row in data table with Standard Value data
DataRow dr = dtFilterDataFromAllData.NewRow();
dr["TabName"] = TabName;
dr["StandardDate"] = strPeriod;
dr["BRTab"] = BRTab;
dr["BRLineItem"] = BRLineItem;
dr["Action"] = strAction;
dr["StandardLineItem"] = StandardLineItem;
dr["StandardValue"] = _StandardValue;
dr["Link"] = "True";
dr["RowNumber"] = RowNumber;
dr["cumulative"] = cumulative;
dtFilterDataFromAllData.Rows.Add(dr);
}
else if (StandardValue == "") //when Standard Value is empty but data found
{
//here updating Standard Value
if (_StandardValue != null && _StandardValue.ToString() != "")
{
StandardValue = _StandardValue;
dtFilterDataFromAllData.AcceptChanges();
}
}
}
}



}

/*
var data1 = dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", ""))
.CopyToDataTable();
*/
}
}
}
}
}
}

private string ProcessValue(string value,string unit, bool allowblank)
{
double _unit = 0;
double number;
double tmpvalue = 0;

//if standard value contains % sign then remove % sign and divide by 100
if (value.Trim().GetWholeNumber() != "")
{
if (value.Contains("%"))
{
if (Double.TryParse(value.Replace("%", string.Empty).ToString(), out number))
{
tmpvalue = number / 100;
}
}
else
{
if (Double.TryParse(value, out number))
{
tmpvalue = Convert.ToDouble(number);
}
}

//if unit blank then set default unit 1 else leave
if (unit.Trim() == "")
{
_unit = 1;
}
else
{
_unit = Convert.ToDouble(unit);
}

//here multiply standard value by unit & return the same
tmpvalue = tmpvalue * _unit;

return tmpvalue.ToString();
}
else
{
if (allowblank)
return "0";
else
return "";
}
}


private void ExtractStandardValueFromChildRow(DataTable dtFilterForLink, DataTable dtFilterDataFromAllData, string TabName,
string StandardLineItem, string BRTab, string RowNumber, string strPeriod, int rowposition,ref string StandardValue)
{
string /*StandardValue = "",*/ strLink = "", strUnits="", _TabName="", _StandardLineItem="";

#region When StandardValue is empty then move to next item

for (int r = rowposition; r <= dtFilterForLink.Rows.Count - 1; r++)
{
_TabName = dtFilterForLink.Rows[r]["Matched Section"].ToString() ?? "";
_StandardLineItem = dtFilterForLink.Rows[r]["Matched Items"].ToString() ?? "";
strLink = dtFilterForLink.Rows[r]["Link"].ToString().ToUpper() ?? "";
BRTab = dtFilterForLink.Rows[r]["Tab"].ToString().ToUpper() ?? "";
RowNumber = dtFilterForLink.Rows[r]["Row"].ToString().ToUpper() ?? "";
strUnits = (dtFilterForLink.Rows[r]["Units"] == DBNull.Value ? "" : dtFilterForLink.Rows[r]["Units"].ToString());

if (TabName.Trim().ToUpper() == _TabName.Trim().ToUpper() && StandardLineItem.Trim().ToUpper() == _StandardLineItem.Trim().ToUpper())
{
if (strLink == "FALSE")
{
//querying data table to find out StandardValue
StandardValue = dtFilterDataFromAllData.AsEnumerable().Where(w => w.Field<string>("BRTab") == BRTab
&& w.Field<string>("RowNumber") == RowNumber
&& w.Field<string>("StandardDate").Replace("A", "").Replace("E", "") == strPeriod.Replace("A", "").Replace("E", "")
&& w.Field<bool>("Link").ToString().ToUpper() == "FALSE")
.Select(v => v.Field<string>("StandardValue"))
.FirstOrDefault();

if (StandardValue != null && StandardValue.ToString() != "")
{
//if Standard Value found then further process the data before return it
StandardValue = ProcessValue(StandardValue ?? "", strUnits,false);
//dtFilterDataFromAllData.AcceptChanges();
//return StandardValue;
break;
}
else
{
//if Standard Value not found then recursively call the same function till TabName & StandardLineItem is same
ExtractStandardValueFromChildRow(dtFilterForLink, dtFilterDataFromAllData, _TabName, _StandardLineItem, BRTab, RowNumber, strPeriod, r + 1, ref StandardValue);
}
}
}
}
#endregion
}

Continue reading...
 
Back
Top