2

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?

Jin
  • 127
  • 3
  • 11
  • What is it really you're trying to do with your code? First you query your data context for some items. _If_ the returned item count is >1, you query your data context _again_ and fetch the first item. And then after that, you run `SingleOrDefault()` on your original query and do some stuff? This doesn't make sense at all... For starters, why do you run the same query twice? And secondly, if you enter your first if-statement, you'll _never_ enter the second one... And what if `matchedprod.Count()` is >2? And you delete only one item? So many questions... – Julian Jan 09 '12 at 06:50

3 Answers3

3

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:

if (matchedprod.SingleOrDefault() != null)
{
    matchedprod.SingleOrDefault().date = price.date;
    matchedprod.SingleOrDefault().prc = price.prc;

    db.SubmitChanges();
    return false;
}

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 matchedprod and matchedprodDel? The queries for them are equal?

Forth:

This is easier to read:

var matchedprod = db.Price.Where(x => x.name == price.name && x.company == price.company && x.date != price.date)
Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Thanks for your help. Did as you said. But the rate has not increased, unfortunately. – Jin Jan 09 '12 at 07:22
  • I said four different things. Which one did you do? – jgauffin Jan 09 '12 at 07:33
  • While optimized for queries. I've updated the code. If something is wrong, correct me. As for your first board, I do not quite understand the implementation. Sorry of course. I am Russian. And I can not catch a quick thought in English ... – Jin Jan 09 '12 at 07:41
2

It runs for a long time, even with the threadpool.

Delusion. If a method takes 10 seconds, it takes 10 seconds in and out of a thread. A thread does not magically improove the speed of a function.

What the threadpool does is that it allows you to run X method calls in parallel, but none of them gets faster in itself.

Now, I would start with the call - see, you have some SQL there that should not take more than a millisecond to execute, so if it is slow, may I suggest either overloaded hardware or some not so smart person not knowing what an index is, making the SQL statements very very slow?

Also note that a thread pool, unless preconfigured, is SLOWLY ramping up threads - like one per second I think. So if you queue a lot... you are dead.

Finally, why the heck do you use a thread pool, and not the tasks library? It is not that new.... and has a nicer API and more control over the threads.

And finally, use a profiler to find out where the time is spent. It is not that hard. Programming without a profiler is like trying to cook without heating anything - you are limited in what you doo. Professional cook? Use professional tools.

Isak Savo
  • 34,957
  • 11
  • 60
  • 92
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • I'm using ThreadPool because I thought for this problem it would be a suitable solution. Please tell me the correct decision? I am very grateful to you for help in solving my problem. – Jin Jan 09 '12 at 07:33
  • Tasks library. Or at least tell the threadpool to use a higher than normal number of starting threads. Plus get at least borderline professional and PUT A PROFILER TO WORK. – TomTom Jan 09 '12 at 08:12
0

Usually speeding up code first involves using a so called "Profiler". This is a tool that actually measures how your code performs (down to the source code line level).

Before doing any optimization, this should always be your first step. My own experiences shown me that the pure guessing what is slow usually is wrong. Instead, profiling helped me to find the real bottlenecks to optimize.

So I suggest that you first use a profiler, personally I'm using ANTS Performance Profiler (14 day free trial), to detect your slow running code lines.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Thank you for your reply. I'll try the profiler. But without it, I can say that the problem is to update the information in the database. When there is a supplement to the database then the addition is rapid. The code that causes the problem I have left. I want to note that the added or updated about 9000 lines. – Jin Jan 09 '12 at 06:45