code optimizations

Member
Posts: 749
Joined: 2003.01
Post: #1
In my programs I tend to create quite a bit of "auxiliary" variables to make the code more readable-easier to write. For instance I could have a function like this


Code:
my_function(x,y)

local a=x+y
local b=x-y
local c=a*a+b*b
return c

end function
(of course not so simple, just to give you the idea)


I was wondering if this, in C/C ++, produced the same binary as

Code:
my_function2(x,y)

return (x+y)*(x+y)+(x-y)*(x-y)

end function


i.e. the compiler optimized the function for you.

(in blitzmax sadly the first function is slower)

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
The former is of course more efficient (who knows what blitz is on). GCC is pretty good at optimization, so I'd be very surprised if it didn't manage to turn the second into the first.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
Code:
[CookieJar:~] keith$ cat > test.c
int function1(int x, int y)
{
    int a = x + y;
    int b = x - y;
    int c = a * a + b * b;
    return c;
}

int function2(int x, int y)
{
    return (x + y) * (x + y) + (x - y) * (x - y);
}
[CookieJar:~] keith$ gcc -O2 -c test.c
[CookieJar:~] keith$ otool -tV test.o
test.o:
(__TEXT,__text) section
_function1:
00000000        subf    r0,r4,r3
00000004        add     r3,r3,r4
00000008        mullw   r0,r0,r0
0000000c        mullw   r3,r3,r3
00000010        add     r3,r3,r0
00000014        blr
_function2:
00000018        subf    r0,r4,r3
0000001c        add     r3,r3,r4
00000020        mullw   r0,r0,r0
00000024        mullw   r3,r3,r3
00000028        add     r3,r3,r0
0000002c        blr

So yes, it turns both into the first quite happily.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #4
Sadness.

Although blitzmax is compiled and very fast at drawing (uses opengl), it looks to me it's pretty badly optimized, especially when you go to oop. Damn.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Moderator
Posts: 529
Joined: 2003.03
Post: #5
Not that I'm qualified to dispute it, but what do you base that on, Matteo?

Incidentally, you could also do it like this:

Code:
my_function2(x,y)

return (x+y)^2+(x-y)^2

end function

Or this:

Code:
my_function2(x,y)

return 2*(x^2+y^2)

end function

Since Blitz requires Apple's dev tools to work, I'd guess it uses gcc somewhere in the process. It can actually compile C/++ code.

"Yes, well, that's the sort of blinkered, Philistine pig-ignorance I've come to expect from you non-creative garbage."
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Blitz doesn't use GCC. It generates assembly itself and then uses the platform's assembler to turn that into machine code.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #7
I did some testing.

Say this:

Code:
function square(x)
return x*x
end function

for i=1 to 100000
a=square(2)
next

went MUCH slower than this

Code:
for i=1 to 100000
a=2*2
next


Also from the example above, it looks like creating auxiliary variables slows it down.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
That's not surprising, function calls generally have a not insignificant overhead. That can be minimized in C/C++ by inlining them, but the chances are that blitzmax doesn't do that kind of optimization (if indeed it does any).

Why are you so concerned with these minutae of performance? My experience is that they really don't matter a great deal, and on the odd occasion when they do, writing that little bit in C is a perfectly good solution.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #9
Also this

Code:
function1 (x)

a=x+3
b=a*5
return b

end function

goes slower than this

Code:
function2 (x)

return (x+3)*5

end function

while they should take the same (I think (??)) if it was optimized.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #10
I'll test these better and take the millisecs when I go home.

I'm concerned because when you start doing OO stuff I'm afraid it loses a LOT of time ( I'm doing computationally intensive stuff)

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #11
Ok, now I have hard numbers:

This:

Code:
Function sq(x#)
Return x*x
End Function

t=MilliSecs()
Local a#

For i=0 To 10000000
a=sq(2)
Next

Print (MilliSecs()-t)

runs in 700 millisecs

While this:

Code:
t=MilliSecs()
Local a#
For i=0 To 10000000
a=2*2
Next
Print (MilliSecs()-t)

runs in 70 millisecs. 'Cmon, it 10 times more!! This discourages the use of functions a lot!

I dont know what C does, but I would be surprised if it took 10 times more.

The second example


Code:
Function f(x#)
Local a#=x+3
Local b#=a*5
Return b

End Function


t=MilliSecs()

Local a#
For i=0 To 10000000
a=f(2)
Next

Print (MilliSecs()-t)


takes 950 millisecs

while



Code:
Function f(x#)

Return (x+3)*5

End Function

t=MilliSecs()

Local a#
For i=0 To 10000000
a=f(2)
Next

Print (MilliSecs()-t)


takes 700 millisecs, while they should be the same if it was decently optimized

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #12
Quite frankly, if you're worried about speed you shouldn't use a basic derivative. You'd use C or C++ with compiler optimizations.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #13
Point taken, though Bmax is a damn good tool otherwise, and the language itself is really cool.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #14
Obviously in Blitzmax there is a large overhead for a function call, like OSC said- since this is an interpreted language that probably doesn't do much optimizations, that is not surprising. If you want most of your examples to run as fast as each other, you need to see if there's a way to inline functions or use macros instead, though it doesn't look (based on a glance of the manual) that they have anything like that.

If you had some time on your hands you could potentially write a Blitz preprocessor that does this macro stuff for you, then all instances of your SQRT function will be replaced by the function body- inlining the functions for you. But yes, it is somewhat absurd the developers of that language haven't implemented anything like that.
Quote this message in a reply
Moderator
Posts: 529
Joined: 2003.03
Post: #15
Are you doing these speed tests with debug build off, Matteo?

"Yes, well, that's the sort of blinkered, Philistine pig-ignorance I've come to expect from you non-creative garbage."
Quote this message in a reply
Post Reply