Testing some singleton implementations
luka8088
luka8088 at owave.net
Sun Feb 9 04:20:54 PST 2014
On 31.1.2014. 9:25, Andrej Mitrovic wrote:
> There was a nice blog-post about implementing low-lock singletons in D, here:
> http://davesdprogramming.wordpress.com/2013/05/06/low-lock-singletons/
>
> One suggestion on Reddit was by dawgfoto (I think this is Martin
> Nowak?), to use atomic primitives instead:
> http://www.reddit.com/r/programming/comments/1droaa/lowlock_singletons_in_d_the_singleton_pattern/c9tmz07
>
> I wanted to benchmark these different approaches. I was expecting
> Martin's implementation to be the fastest one, but on my machine
> (Athlon II X4 620 - 2.61GHz) the implementation in the blog post turns
> out to be the fastest one. I'm wondering whether my test case is
> flawed in some way. Btw, I think we should put an implementation of
> this into Phobos.
>
> The timings on my machine:
>
> Test time for LockSingleton: 542 msecs.
> Test time for SyncSingleton: 20 msecs.
> Test time for AtomicSingleton: 755 msecs.
>
What about swapping function pointer so the check is done only once per
thread? (Thread is tldr so I am sorry if someone already suggested this)
--------------------------------------------------
class FunctionPointerSingleton {
private static __gshared typeof(this) instance_;
// tls
@property static typeof(this) function () get;
static this () {
get = {
synchronized {
if (instance_ is null)
instance_ = new typeof(this)();
get = { return instance_; };
return instance_;
}
};
}
}
--------------------------------------------------
dmd -release -inline -O -noboundscheck -unittest -run singleton.d
Test time for LockSingleton: 901 msecs.
Test time for SyncSingleton: 20.75 msecs.
Test time for AtomicSingleton: 169 msecs.
Test time for FunctionPointerSingleton: 7.5 msecs.
I don't have such a muscular machine xD
More information about the Digitalmars-d
mailing list