Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math library against own library in C#

So we tried developing a math class in C# and we did. Comparing results with the original math class for System.Math shows that we are always a little or a lot slower (trig methods particularly).

But the wonder comes when we are using basic methods like absolute value which does not contain loads of code apart from

if(value < 0) return -value;
else return value;

and still we are far behind.

I cannot make this abs method any smaller, using the ternary operator will not help either I guess.

Is it because the System.Math would be written in C? Would it go faster if we write it in native language, though it seems it won't change much I read. Finally, could a dll work faster than a class and if so why and if not… well why too?

like image 723
Everts Avatar asked Nov 30 '25 14:11

Everts


1 Answers

Continuing with Servé's comment that shows the CLR is written in C++, you'll find that Math.Abs calls fabs or fabsf.

FCIntrinsicSig("Abs", &gsig_SM_Flt_RetFlt, COMDouble::AbsFlt, CORINFO_INTRINSIC_Abs)
FCIntrinsicSig("Abs", &gsig_SM_Dbl_RetDbl, COMDouble::AbsDbl, CORINFO_INTRINSIC_Abs)
/*=====================================AbsFlt=====================================
**
==============================================================================*/
FCIMPL1_V(float, COMDouble::AbsFlt, float f) 
    WRAPPER_CONTRACT;
    STATIC_CONTRACT_SO_TOLERANT;

    FCUnique(0x14);
    return fabsf(f);
FCIMPLEND

/*=====================================AbsDbl=====================================
**
==============================================================================*/
FCIMPL1_V(double, COMDouble::AbsDbl, double d) 
    WRAPPER_CONTRACT;
    STATIC_CONTRACT_SO_TOLERANT;

    return fabs(d);
FCIMPLEND
like image 84
user247702 Avatar answered Dec 03 '25 07:12

user247702



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!