Saturday, May 12, 2012

Semaphore vs SemaphoreSlim. Preliminary results.

I'm thinking about creating new cool asynchronous library.

Asynchronicity has its price. You may delegate the work to a background thread, but there is a price to pay. What's the price?

The question was: what's faster: Semaphore or brand new SemaphoreSlim (and accompanied EventWaitHandle vs ManualResetEventSlim)? (Yes, there are more components of the price, this is just one of them.)

Actually, in the very beginning I wanted to compare how well parallel things are built into Objective-C and Macs. After all, they have message passing in the very language, that must be implemented well. What's cooler, Objective-C or .Net Framework? I knew I wouldn't change the platform anyway, but this was the thing I wanted to know.

I put my hands into code, created the test project. Then I ran it on few available and very different machines.

The idea was to get the price of offloading work itself. For that kind of work, I choose "find me a prime that is >= the given number". Numbers in question were 2, 200 and 20,000. That work was offloaded 50,000 times.

For asynchronous algorithms the loop was:
for i in 0..49999
    offload work to thread (i % number_of_threads)
    wait for the work to complete

This approach, by the way, gives no benefit of having many threads. The idea was to benchmark offloading price itself.

I obtained results I can't explain. Here are they.

In the first column you see the algorithm: "Direct" (time of running the loop directly), "Slim" and "Trad". Cells with purple background highlight the cases where traditional synchronization primitives outperformed "slim" ones. Third column shows the number of background threads.

Systems were different. They have different OS (yet all up-to-date), .Net Frameworks, FSB and RAM timings.
"kserver" is a very old machine, amd, single core (~1.8 Ghz), runs Windows XP SP3, 32 bit.
"bhuge", with Core2Duo, runs Windows XP SP3, 32 bit.
"inspiron", with Core2Duo, runs Windows 8 developer preview, 64bit and has .Net 4.5 Beta.
"xps", with i7-2630QM, runs Windows 7 Home Premium 64 bit.

I didn't have the time to take a look at FSB frequency and DRAM timings.

What's clear is: SemaphoreSlim doesn't always outperform Semaphore.

Strange thing is: amd turned out to be the fasters to switch between threads, and i7 – the slowest. Very strange.

No comments:

Post a Comment