Is external C code slower ?

BlitzMax Forums/BlitzMax Programming/Is external C code slower ?

Armitage 1982(Posted 2009) [#1]
Hi

I'm using Ease Equations for every Tweening effects of my game.
For practical reasons every Tweening functions are store in a C file and called externally.

The most used function is a very simple Linear code :

double LinearTweening(double t, double b, double c, double d)
{
	return c*(t/=d) + b;
}


1) Do you think it would be faster to directly convert this function to BlitzMax ?

2) Equations use Double precision numbers but most of the tweened values are Float.
I suppose that implicitly casting from float to double then from double to float is slow and maybe useless ?
I don't know if Tweening Equations really need Double precision.

What do you think ?

Thanks


Gabriel(Posted 2009) [#2]
1) No, I don't think so. The compilers are different but they both use the same linker and assembler so the only reason I can think of which would make it faster is if the BlitzMax compiler were more optimized than the GCC one. I doubt that's the case. I suspect they're both pretty similar actually. I wrote all mine in BlitzMax but I very much doubt they're faster than yours.

2) I don't see any need for double precision here, no. Even less so if you're going to be using single precision most of the rest of the time. I implement all my tweening functions with single precision, and they're fine for my purposes. Why not implement two versions and use the double precision versions only if you find a need for them later. It can't be much work to copy and paste all those functions and then run a find and replace to switch double for float.


Brucey(Posted 2009) [#3]
Do you think it would be faster to directly convert this function to BlitzMax ?

Well, you might want to try it with a normal build and with the compiler optimisations ramped up to full, as well as in BlitzMax code.
Benchmarking will give you an idea which is better. I doubt there'd be too much difference unless you are doing a lot of math - in which case the C might optimise well for FPUs. (SSE/SSE2/etc)


Armitage 1982(Posted 2009) [#4]
Thanks
Following your advices Brucey I did a quick and rough Bench and here the result for 10.000.000 calls
BlitzMax Double: 0.34431778340543279
BlitzMax Float: 0.17504291746576731
BlitzMax Float Casting Double: 0.34422810720356917

C Double: 0.30186210817296610
C Float: 0.18841137630620652
C Float Casting Double: 0.31622594491758033


Value differs from time to time but the Float BlitzMax converted function is always faster than any others.
Using Double or Float to Double Casting cost is x2 but still trivial for 10 millions calls (I doubt I'm calling Tweening functions more than 5000 even with my particles engines).

I will transform my Tweening code to Float precision as you suggest Gabriel and convert my Linear Function in BlitzMax since it's a bit faster.

Thanks for your suggestion :)


Armitage 1982(Posted 2009) [#5]
With QueryPerformanceCounter I see nearly no differences between a tweening engine in Float or in Double mode event by using a BlitzMax Linear function :)
So no real differences but good to know just in case :)


Nate the Great(Posted 2009) [#6]
I also noticed this when writing my physics engine, I assumed c would be faster so I put all of the maths in a c lib and compiled it and it ran much slower than it did in bmax... but maybe thats because I had to use cos, sin, atan(y/x) and sqrt a lot