Shop OBEX P1 Docs P2 Docs Learn Events
A little surprised — Parallax Forums

A little surprised

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

  • ersmithersmith Posts: 6,030

    "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.

  • MicksterMickster Posts: 2,681

    @ersmith said:
    "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

  • ersmithersmith Posts: 6,030

    @Mickster said:
    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?

    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.

  • MicksterMickster Posts: 2,681
    edited 2024-09-26 14:49

    @ersmith said:

    @Mickster said:
    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?

    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 :+1:

    Thanks Eric.

    Craig

Sign In or Register to comment.