Allan
asked on
A More Efficient Way To Write This Method
Hi Experts!
Thanks for reading this.
There has to be a more efficient way of handing the string manipulation.
One thought is it use StringBuilder and passing it as ref..
Each time the method is called it creates the parameter (StrTemp), if I'm not mistaken.
The following method (ConvertStringWithSomeLogic ) gets called 130K times and it takes 2+ hrs to complete..
Here's how the method is being called:
Here's the method being called:
Here are the Convert Segment methods:
TIA!!
Thanks for reading this.
There has to be a more efficient way of handing the string manipulation.
One thought is it use StringBuilder and passing it as ref..
Each time the method is called it creates the parameter (StrTemp), if I'm not mistaken.
The following method (ConvertStringWithSomeLogic
Here's how the method is being called:
private static void InsertIntoDBTable(List<Model3> MdlCombine)
{
SomeDB db = new SomeDB();
foreach (Model3 m in MdlCombine)
{
Model3 st = new Model3();
st.packagecodeid = ConvertStringWithSomeLogic(m.packagecodeid);
st.STARTDATE = (m.STARTDATE);
st.ENDDATE = (m.ENDDATE);
db.Model3s.Add(st);
}
db.SaveChanges();
}
Here's the method being called:
private static string ConvertStringWithSomeLogic(string StrTemp)
{
string strReturn = StrTemp;
try
{
int IntFirstDash = 0;
int IntSecondDash = 0;
GetDashPos(ref IntFirstDash, ref IntSecondDash, StrTemp);
string strFirstSegment = StrTemp.Substring(0, IntFirstDash);
string strSecondSegment = StrTemp.Substring(IntFirstDash + 1, IntSecondDash - (IntFirstDash + 1));
string strThirdSegment = StrTemp.Substring(IntSecondDash + 1, StrTemp.Length - (IntSecondDash + 1));
//CONVERT THE FIRST, SECOND, THIRD SEGMENTS
ConvertFirstSegment(ref strFirstSegment);
ConvertSecondSegment(ref strSecondSegment);
ConvertThirdSegment(ref strThirdSegment);
strReturn = strFirstSegment + strSecondSegment + strThirdSegment;
if ((strFirstSegment.Length < 4) || (strSecondSegment.Length < 3) || (strThirdSegment.Length < 1))
{
strReturn = StrTemp;
//write msg to event log with StrTemp in it
}
}
catch ....
return (strReturn);
}
Here are the Convert Segment methods:
private static void ConvertFirstSegment(ref string FirstSeg)
{
if (FirstSeg.Length == 4)
{
FirstSeg = "0" + FirstSeg;
}
}
private static void ConvertSecondSegment(ref string SecondSeg)
{
if (SecondSeg.Length == 3)
{
SecondSeg = "0" + SecondSeg;
}
}
private static void ConvertThirdSegment(ref string ThirdSeg)
{
if (ThirdSeg.Length == 1)
{
ThirdSeg = "0" + ThirdSeg;
}
}
TIA!!
ASKER
Thanks James; let me try it now ..
ASKER
I thought it was the method, but it's actually looping through the List of Model that's taking while to complete 130K records....
I'm going to catch some zzzzz....
I'm going to catch some zzzzz....
Hi allanau20;
If you are trying to process 130K records to the database using Entity Framework then the most likely cause of the long time is Entity Framework. When Entity Framework does create, update and delete records it sends a single command for each record in the 130K records. Is this what you are attempting to do?
If you are trying to process 130K records to the database using Entity Framework then the most likely cause of the long time is Entity Framework. When Entity Framework does create, update and delete records it sends a single command for each record in the 130K records. Is this what you are attempting to do?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey FernandoSoto.
Ok, I'll look into it in a few hours ...Thanks again.
Ok, I'll look into it in a few hours ...Thanks again.
You may wish to turn off DetectChanges when adding large numbers of items to DBContext (I assume it's a DBContext?)
http://blog.oneunicorn.com/2012/03/12/secrets-of-detectchanges-part-3-switching-off-automatic-detectchanges/
http://blog.oneunicorn.com/2012/03/12/secrets-of-detectchanges-part-3-switching-off-automatic-detectchanges/
ASKER
Hi PaulHews; yes, it's DBContext; I'll look into it. Thanks again.
Turning off DetectChanges will not help in this case because most of your time will be spent sending a single record at a time to the SQL server and getting a response back for each of the 130K records. That is a whole lot of network congestion.
ASKER
I tried the DetectChanges and that didn't speed things up; thanks though PaulHews .
ASKER
I'm sooo stoked; I figured out how to applied the example. LOL..
It works; this is soo cool .. now it runs under a minute... Thanks again FernandoSoto ... lunch on me if you're in town...
It works; this is soo cool .. now it runs under a minute... Thanks again FernandoSoto ... lunch on me if you're in town...
ASKER
awesome!
Not a problem allanau20, glad I was able to help.
Hay, what town would that be. LOL.
Hay, what town would that be. LOL.
ASKER
check your gmail...
I have just minimised the code down to this:
Open in new window
and it executed the function 130k times in about 40ms...
James