English 中文(简体)
Most suitable .net Timer for a scheduler
原标题:

We ve identified a hotspot in our code using CCR timers. It appears that if we enqueue many thousands of timers that the code suffers terminal slowdown.

The fix is to choose the soonest scheduled item and enqueue a timer for this event. When it fires, we repeat. In this way, we re only ever enqueueing one timer interval at a time.

What we re finding now is that the SortedList instance which we re using to manage the scheduled items is burning with the weight of the removals from the list.

Do all .net timers suffer from the problem of increased CPU usage with the number of items enqueued, or is there one that is more intelligently written.

Alternatively, is there a better suited data structure for keeping our scheduled items in ordered fashion, that supports fast insertion and fast removal from the front of the list?

最佳回答

Try using a LinkedList<T>, I m sure you ll see a huge improvement in performance. It s well suited for quick adds and removes.

问题回答

A sorted list uses an array underneath, so removal from the front of the list is one of it s slowest operations. Sorting in reverse order would reduce the time it takes to remove an item from a list, because you d be removing from the end.

If all you care about is the next item, a Priority Queue is probably a good bet.

Sounds like you need to use a priority queue. Priority queues are often implemented with heaps, but I find that skip lists tend to work better. Unfortunately there is no priority queue builtin to the BCL so you will have to write your own or find one elsewhere. That SortedList is going to kill your performance. Even if you decide against the priority queue you could probably get a quick boost by moving to a SortedDictionary instead.

Edit: The reason why I would prefer a priority queue here as opposed to the SortedDictionary is because removals of the lowest key in a SortedDictionary are O(log(n)) as opposed to O(1) for the priority queue.

For a quick fix that might (or might not) be enough, switch to a sorted dictionary.

A sorted list is going to cause problems because its underlying data structure is an array. As another poster pointed out, if you remove from the "top" ([0]) of the sorted list, all the other items have to be shifted down one in the array, so the more items you have in the list, the more expensive it is to remove one. (so the performance is O(n) or worse).

Conversly, insertion and removal on the sorted dictionary is O(log n), so your remove performance should be much less impacted when you have lots of items in the list.

To always get the soonest scheduled item you need to use Heap, rather than SortedList.

There s no heap in standard .Net library. You can download the implementation from here or roll your own.

If you absolutely need the sorting feature you could consider using a SortedDictionary instead of a SortedList. SortedDictionary is O(log n) for insertion and remonal since internally it uses a tree.

http://msdn.microsoft.com/en-us/library/5z658b67(VS.80).aspx





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签