Optimization IS important

About advanced scenes, and the Thyme scripting language used in Algodoo.

Optimization IS important

Postby Kilinich » Mon Oct 31, 2011 2:01 pm

I just can't stand that! WTF is that in "Some extra math functions":

Code: Select all
scene.my.sign = (number) =>
{
    sign := 0;
    (number<0)?{sign=(-1)}:false;
    (number>0)?{sign=1}:false;
    sign;
};
scene.my.abs = (number) =>
{
    number*scene.my.sign(number);
};


it's crazy. who did that? :crazy:
I'll just re-write it:

Code: Select all
scene.my.sign = (x) => {x < 0 ? -1 : 1};
scene.my.abs = (x) => {x < 0 ? -x : x};

See the difference? KISS, and your thyme code will work as fast as a rocket.
Last edited by Kilinich on Fri Jan 06, 2012 8:32 pm, edited 1 time in total.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Optimization IS important

Postby Livirus » Sat Nov 19, 2011 2:32 am

See the difference?

Yeah your sign(0) returns -1.

Code: Select all
scene.my.sign = (x) => {x < 0 ? -1 : 1};
scene.my.abs = (x) => { (x^2)^.5 }

...might be preferable, since sign(0) would now return 1 and abs does no longer require an if-statement.
User avatar
Livirus
 
Posts: 19
Joined: Fri Mar 26, 2010 7:08 pm

Re: Optimization IS important

Postby KarateBrot » Sat Nov 19, 2011 3:03 am

Livirus wrote:...might be preferable, since sign(0) would now return 1

sign(0) should return 0 by definition.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Optimization IS important

Postby Livirus » Sat Nov 19, 2011 3:28 am

KarateBrot wrote:
Livirus wrote:...might be preferable, since sign(0) would now return 1

sign(0) should return 0 by definition.

Ye I know. But if you can live without the 0-exception, I think returning 1 is preferable. It's situational though.
User avatar
Livirus
 
Posts: 19
Joined: Fri Mar 26, 2010 7:08 pm

Re: Optimization IS important

Postby tatt61880 » Sat Nov 19, 2011 4:10 am

Added Kilinich's definitions.
Some extra math functions
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Optimization IS important

Postby Livirus » Sat Nov 19, 2011 4:41 am

I was playing around with math and noticed (0^x)^x returns 1 for all positives, 0 for zero, and +inf for all negatives. So all we need is an exception for x<0 and we're done :) :
Code: Select all
scene.my.sign = (x) => { x < 0 ? {-1} : 0^(0^x) };

It seem to run perfectly, including on zero.

Short? :thumbup:
Simple? :thumbup:
Fast? :problem:

EDIT:
Here's an option:
Code: Select all
scene.my.sign = (x) => { x != 0 ? x/(x^2)^.5 : 0 };
User avatar
Livirus
 
Posts: 19
Joined: Fri Mar 26, 2010 7:08 pm

Re: Optimization IS important

Postby tatt61880 » Sat Nov 19, 2011 5:54 am

I think, which original scene.my.sign is defined only for scene.my.abs.
So, we seldom use sign() function which requires sign(0) = 0, don't we?
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Optimization IS important

Postby Livirus » Sat Nov 19, 2011 6:58 am

tatt61880 wrote:I think, which original scene.my.sign is defined only for scene.my.abs.
So, we seldom use sign() function which requires sign(0) = 0, don't we?

Are you saying abs(0)=0*sign(0)=0 despite sign(0)=1, thus sign(0)=0 is not required?
As both Kilinich and I have demonstrated more efficient abs-functions that are independant of the sign-function, how does that make any sense? :?
And why would the abs-function be the only application of the sign-function? :?
User avatar
Livirus
 
Posts: 19
Joined: Fri Mar 26, 2010 7:08 pm

Re: Optimization IS important

Postby tatt61880 » Sat Nov 19, 2011 8:21 am

Livirus wrote:
tatt61880 wrote:I think, which original scene.my.sign is defined only for scene.my.abs.
So, we seldom use sign() function which requires sign(0) = 0, don't we?

Are you saying abs(0)=0*sign(0)=0 despite sign(0)=1, thus sign(0)=0 is not required?
As both Kilinich and I have demonstrated more efficient abs-functions that are independant of the sign-function, how does that make any sense? :?
And why would the abs-function be the only application of the sign-function? :?


When do you use sign() function?
I think sign() which sign(0) should be 0 is very rare to be used.
Even sign() which allow sign(0)!=0 is rare to be used.
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: Optimization IS important

Postby KarateBrot » Sat Nov 19, 2011 12:56 pm

sorry i didn't want to start a discussion :D i just read that for programming 0 often gets let out so that it only takes one bit for calculations so there's no problem
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Optimization IS important

Postby highly » Fri Dec 30, 2011 3:30 am

Livirus wrote:
See the difference?

Yeah your sign(0) returns -1.

Code: Select all
scene.my.sign = (x) => {x < 0 ? -1 : 1};
scene.my.abs = (x) => { (x^2)^.5 }

...might be preferable, since sign(0) would now return 1 and abs does no longer require an if-statement.


How is scene.my.abs = (x) => { (x^2)^.5 } preferable? it's MUCH less efficient than other methods :/

Scene.my.abs = (x) => {x < 0 ? (x * (-1)):x} works for every situation. I also ran these through multiple iterations tests. for the purpose of the test, I use abs1 = (x) => {x < 0 ? (x * (-1)):x} and abs2 = (x) => { (x^2)^.5 }

Code: Select all
for 1000 iterations, 2000 tests each per iteration:
     abs1: 404ms
     abs2: 1157ms


I used an array of random floating point numbers in the range of -1000 to 1000 to test. EACH function was tested 2,000,000 times, and I calculated the computation time by using milliseconds from the system date and time.

As you can see, your proposed "preferred" method takes twice as long as the method I've shown. When you do (x^2)^.5, you perform relatively expensive power operations, and the .5 translates into a square root calculation, which is INCREDIBLY expensive to compute.

The method I used for abs1 is also all inclusive, it works for every possible number.

To show the difference between the methods, here's a test that goes through 5,000,000 iterations:

Code: Select all
for 2500 iterations, 2000 tests each per iteration:
     abs1: 966ms
     abs2: 2853ms


Over a larger sample we see that the abs function you seem to consider "preferable" is very close to 3 times slower than mine. Not including the "if" statement in NO way makes it preferable, "ease of writing" should never be favored if you're giving up almost 3 times as much processing time.
User avatar
highly
 
Posts: 1
Joined: Fri Dec 30, 2011 3:07 am

Re: Optimization IS important

Postby Kilinich » Fri Jan 06, 2012 8:35 pm

I've fix sign(0), thanks.
I usually call it as {value^2 * sign(value)} so sign(0) is not important in that case... ;)
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 6 guests