A little surprised
Mickster
Posts: 2,698
const _clkfreq = 300_000_000 dim as integer a, b, c, d dim as integer i,strt,fini b=12 strt=getus() for i = 1 to 10000 c=b^2 next fini=getus() print c print "execution: "; (fini-strt)
execution: 112801
const _clkfreq = 300_000_000 dim as integer a, b, c, d dim as integer i,strt,fini b=12 strt=getus() for i = 1 to 10000 c=b*b next fini=getus() print c print "execution: "; (fini-strt)
execution: 4001
Is the "b^2" not the correct syntax?
Craig
Comments
"b^2" is the correct syntax, but it's a floating point operator, so it converts its operands to float. That's why it's much slower than integer multiplication.
Hi Eric,
My other tests seem to indicate that integers multiply five times faster than floats but here we are talking twenty-eight times(?)
Is it the converting that makes the huge difference?
Regards,
Craig
Exponentiation isn't just multiplication, it also has to handle fractional exponents (like 0.5 for square root). So it's actually fairly messy.
We could probably try to optimize some common cases, but on the other hand it's fairly easy for the programmer to do that too.
Oh it's not a problem now that I know that it's not a bug. I just assumed that b^2 would be more efficient than b*b but then again, this is why I'm testing everything that I do.
Such a joy to use FlexBasic
Thanks Eric.
Craig