With this code vozhus very much in it for a long time tinkering that he worked steadily and rapidly. But can not seem to speed it up. Is slow …
for (int jrCnt = rCnt; jrCnt <= arrayTable.GetUpperBound(0); jrCnt++)
{
var prcI = new Price();
/* here is the code search and add data to prcI */
if ((!string.IsNullOrEmpty(prcI.name)) && (prcI.prc != 0))
{ // function add
/* adding more information to prcI */
ThreadPool.QueueUserWorkItem(delegate
{
if (!Accessor.AddProductUpdateProduct(prcI)) _updateCounter++;
_countadd++;
}); // I put the longest function in the streams
}
}
Here we call the function. It runs for a long time, even with the threadpool.
public static bool AddProductUpdateProduct(Price price)
{
using (var db = new PriceDataContext())
{
var matchedprod =
db.Price.Where(x => x.name == price.name && x.company == price.company && x.date != price.date);
if (matchedprod.Select(x=>x).Count() > 1)
{
db.Price.DeleteOnSubmit(matchedprod.First());
db.SubmitChanges();
}
var matchedproduct = matchedprod.SingleOrDefault();
if (matchedproduct != null)
{
matchedproduct.date = price.date;
matchedproduct.prc = price.prc;
db.SubmitChanges();
return false;
}
}
/*here the code to add the product to the database.*/
return true;
}
Please tell me how to speed up the work with the threadpool?
Using a separate thread doesn’t speed anything app. All you do is to move the processing from the primary thread to another one. You need to break the processing into smaller parts and move each part to a separate thread to get any performance gain.
However, it will not help in this case. It’s your LINQ queries that are flawed. Enable debugging. Look at the generated SQL and fix them.
Secondly:
That will query the database three times. Once per call to SingleOrDefault. Do one query and store the result in a variable.
Third:
What’s the difference between
matchedprodandmatchedprodDel? The queries for them are equal?Forth:
This is easier to read: