3

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?

7
  • Above all, it is just more readable.
    – Itay
    Commented Sep 18, 2013 at 11:11
  • 2
    Did you NGEN your library? Because System.Math has native images which is faster Commented Sep 18, 2013 at 11:14
  • 1
    Your absolute value implementation contains a bug, by the way.
    – AakashM
    Commented Sep 18, 2013 at 11:14
  • 1
    what about int.MinValue? :) Commented Sep 18, 2013 at 11:15
  • There may be a bug, it was more of an example. I may have a look at the NGEN, though I think in the end we will use the System.Math, it was more of a trial.
    – Everts
    Commented Sep 18, 2013 at 12:07

2 Answers 2

6

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
1

I don't think trivial functions in System.Math are written in C because the overhead of calling out to C would be in many cases much too high. Maybe they are written in raw IL to make them extra fast.

BTW: Why would you write your own math library? This is almost never a good idea.

2
  • 2
    stackoverflow.com/questions/8870442/… Math is written in C++ Commented Sep 18, 2013 at 11:17
  • It is part of a project for learning. Indeed, there is no reason to do so since System.Math is already having all that is needed, but it was just a way to learn Taylor series and other algorithm and see how methods are implemented.
    – Everts
    Commented Sep 18, 2013 at 12:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.