and in the loop I have:
//this gives approx 8000 per sec
float heading = atan2(rand(), rand());
//this gives approx 3500 per sec
float heading = mag.GetHeading();
Without looking at a complete source, I see:
You get 8000 per sec with three function calls:
And you get 3500 per sec with four function calls:
So the single function call "mag.GetHeading()" costs about as much time as rand(),rand(),atan2() together. I don't know why there is such a big difference. But perhaps the "heading" result is never ever used in the sketch, so the function calls of rand() and atan2() that are called from the ino-sketch can be eliminated from the executable.
For better comparison (and slower execution, less optimization possible) perhaps declare "heading" as a "volatile" variable, add up the results during timing and use the result.
volatile float heading1, heading2; // make results "volatile"
//use for timing this:
heading1 += atan2(rand(), rand()); // add up during timing
//and for the other timing that:
heading2 += mag.GetHeading(); // add up during timing
How much difference will you see then?