views:

161

answers:

3

For a single web request, We fetch more than 1000 rows of data from an external system and we need to re-format the data into much better presentation format before we display it to the end user on the web page.

When we loop through the data in ASPX pages, it is creating a hard loop (or tight loop?) which is consuming more than 95% of the CPU.

How do we avoid CPU spikes and also have the process completed as fast as possible? TIA

+4  A: 
  1. Buy a faster server
  2. Buy more servers
  3. Do less work

Are you duplicating any effort here? Is it the same 1000 rows on subsequent requests for the same data? Then maybe you should cache the results? Are some of the calculations the same, over and over? Then maybe you should store the results of the calculations along with the raw data.

Bottom line: don't do a tight loop in the presentation layer. It doesn't belong there.

It's also hurting your scalability, BTW. You're tying up worker threads that could be handling other requests, perhaps other requests not sitting in tight CPU loops.

John Saunders
Thx, We are currently looking at using a caching solution. Curious as to why you say tight loop in the presentation layer is not correct, in our case the looping is to format the data into more presentable format (or which layer do tight loops go into, if one is required?)TIA
Kishork
It's more that it's not correct in an ASP.NET program. You're tying up a scarce worker thread during this request, and this work should almost certainly be done elsewhere. To what extent is the tight CPU loop influenced by the particulars of the request? Does the request change which 1000 rows is processed? Does the request change the algorithm used in the tight loop? Is the processing of each of the rows independent of the other rows? If so, then you should format the rows ahead of time, then just return pre-formatted rows.
John Saunders
+1  A: 

Building on John's #3, do you really need 1000 rows of data? That sounds like a lot. Users won't appreciate having to sort through that manually. Consider offering paging or search features that reduce the amount shown on the screen at one time. If you're already compiling that down to a smaller number of records, you can probably do it more efficiently in your database.

Joel Coehoorn
@Joel, I was hoping he had to do a lot of processing on the 1000 rows, and would then display a lot fewer than 1000 rows. If he's really displaying all 1000, _after_ doing a lot of crunching on them, then, *ouch*.
John Saunders
+1  A: 

Unless you're doing a lot of really complicated stuff, I'd wonder how you're noticing your CPU spinning for an noticeable period of time crunching only 1000 rows. You can do some serious work on a modern server in a very short period of time so long as the data is already in memory (which it sounds like it is in your case).

Are you doing things like repeatedly iterating over the items (ie. nested loops) when using a dictionary would work better? Take a close look at what you're doing and consider how many times each line of code actually runs.

Some code I worked on last week had to evaluate a series of complex security rules on about 500,000 objects. The end code took about 3.5 minutes to run -- 2 minutes to load from the DB, 10 seconds to process, and 1 minute 20 seconds to write the results out to the result file. Once you have everyting in memory, if you make sure you don't loop over data more times than you need, things can be surprisingly fast.

Jonathan