std.variant benchmark
jerro
a at a.com
Thu Aug 2 21:28:18 PDT 2012
On Sunday, 29 July 2012 at 20:24:42 UTC, Dmitry Olshansky wrote:
> On 30-Jul-12 00:11, Dmitry Olshansky wrote:
>> I've found something far more evil:
>>
>> @property bool convertsTo(T)() const
>> {
>> TypeInfo info = typeid(T);
>> return fptr(OpID.testConversion, null, &info) == 0;
>> }
>>
>> Okay... now let me pull off another piece of rag:
>>
>> private VariantN opArithmetic(T, string op)(T other)
>> {
>> VariantN result;
>> static if (is(T == VariantN))
>> {
>> /*if (convertsTo!(uint) && other.convertsTo!(uint))
>> result = mixin("get!(uint) " ~ op ~ "
>> other.get!(uint)");
>> else*/ if (convertsTo!(int) &&
>> other.convertsTo!(int))
>> result = mixin("get!(int) " ~ op ~ "
>> other.get!(int)");
> ...
> Apparently I'm spot on.
> Just commenting one extra branch of this horror movie
> gives interesting change:
>
> 2779us
> 2667us
> 3153762us
>
> After:
>
> 2319us
> 2523us
> 288581us
>
> Aye, 10x :o)
I profiled it and found out much of the time is spent inside
TypeInfo.opEquals being called from tryPutting. So I tried
replacing "!= typeid" in tryPutting with "!is typeid". That
brought the time from 2.8 s down to 0.12 on my machine. I don't
know if that is the proper solution, since I don't know if typeid
can ever return two TypeInfo objects that aren't the same but are
equal (I haven't used typeid and TypeInfo much before). The fib
function here does return correct result after doing that, though.
More information about the Digitalmars-d
mailing list