How sweet Parallel.For is!

Currently one of my tasks is to determine the optimal fill-factor for SQL Server indexes. Going with the default 100% for some indexes leaves a big hole in the foot. The chart below illustrates how the performance deteriorates the longer the time between index defragmentation is (i.e. the higher growth percentage).


As you can see, the curves are complex. If you go for a low Fill Factor (12.5%), you may have an index that is 8 times bigger (700% more time). If you pick a high percentage and do not defrag often, the performance will go off the chart because of fragmentation (and thus spindle movements – a major latency factor).


But I diverge, to produce the above charts I need to do a lot of calculations. My first cut was just doing a loop and it took forever. I’d recently attended a talk on .Net4 and it’s parallel features, so I did a little code modification as shown below. I created an IEnumerable (lList) of the values that would have gone into my for. I created an array to receive the results into.  Then I fired it up…


   1: var dataList = new RunData[Loops];
   2: var loops =new List<int>();
   3: for (var pass = 0; pass < Loops; pass++)
   4:     loops.Add(pass);
   6:   FillExtents(new RunData()
   7: {
   8:     FilledSlots = usecase.FilledSlots,
   9:     InitialRecords = usecase.InitialRecords,
  10:     RecordsToAdd =1000,
  11:     Slots = usecase.Slots,
  12:     TotalExtents = usecase.TotalExtents
  13: });
  15: Parallel.ForEach(loops, (int  pass) =>
  16: {
  17:     dataList[pass]=
  18:         FillExtents(new RunData()
  19:                         {
  20:                             FilledSlots = usecase.FilledSlots,
  21:                             InitialRecords = usecase.InitialRecords,
  22:                             RecordsToAdd = usecase.RecordsToAdd,
  23:                             Slots = usecase.Slots,
  24:                             TotalExtents = usecase.TotalExtents
  25:                         });
  26: }
  27:     );

Instead of seeing a single processor cranking away, I suddenly see 8 CPUs in the task manager running at 75%. Needless to say, a 87.5% improvement in performance is sweet!

Converting to parallel processing just takes a little fore-thought…. then the coding with .Net4 is sweet, no threads to manage or other joys…


Popular posts from this blog

Yet once more into the breech (of altered programming logic)

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

How to convert SVG data to a Png Image file Using InkScape