Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Question about Programming Logic  (Read 1226 times)

0 Members and 1 Guest are viewing this topic.
Question about Programming Logic
« on: July 18, 2008, 04:41:31 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I was going to see if I could do grid movement without the standard grid that Game Maker has, but I have questions.  The method I was using involves modulus on both the x and y variables of the moving object; this is because modulus returns the remainder and if I have a 16 x 16 grid layout, taking both the x and y modulo 16 returning 0 will mean that the moving object is in fact on the grid:

Code: [Select]
if (!(x mod 16 = 0 && y mod 16 = 0))
{
...
}

or this:
Code: [Select]
if (x mod 16 != 0 || y mod 16 != 0)
{
...
}

My question is in relation to De Morgan logic.  Even though both statements are the same in what they return, which is more efficient to use, or is it just a stylistic choice?  My guess would be that the or is quicker because it only has to check one, but I am unsure.
Logged

Mamoruanime

@Mamoruanime
Re: Question about Programming Logic
« Reply #1 on: July 18, 2008, 04:49:26 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
I was going to see if I could do grid movement without the standard grid that Game Maker has, but I have questions.  The method I was using involves modulus on both the x and y variables of the moving object; this is because modulus returns the remainder and if I have a 16 x 16 grid layout, taking both the x and y modulo 16 returning 0 will mean that the moving object is in fact on the grid:

Code: [Select]
if (!(x mod 16 = 0 && y mod 16 = 0))
{
...
}

or this:
Code: [Select]
if (x mod 16 != 0 || y mod 16 != 0)
{
...
}

My question is in relation to De Morgan logic.  Even though both statements are the same in what they return, which is more efficient to use, or is it just a stylistic choice?  My guess would be that the or is quicker because it only has to check one, but I am unsure.

Honestly with it being Gamemaker I wouldn't worry about which is quicker; it's not going to make a noticeable difference in performance anyway. I'd say just go with whichever you feel most comfortable with.
Logged
Re: Question about Programming Logic
« Reply #2 on: July 18, 2008, 04:54:45 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
I was going to see if I could do grid movement without the standard grid that Game Maker has, but I have questions.  The method I was using involves modulus on both the x and y variables of the moving object; this is because modulus returns the remainder and if I have a 16 x 16 grid layout, taking both the x and y modulo 16 returning 0 will mean that the moving object is in fact on the grid:

Code: [Select]
if (!(x mod 16 = 0 && y mod 16 = 0))
{
...
}

or this:
Code: [Select]
if (x mod 16 != 0 || y mod 16 != 0)
{
...
}

My question is in relation to De Morgan logic.  Even though both statements are the same in what they return, which is more efficient to use, or is it just a stylistic choice?  My guess would be that the or is quicker because it only has to check one, but I am unsure.

Honestly with it being Gamemaker I wouldn't worry about which is quicker; it's not going to make a noticeable difference in performance anyway. I'd say just go with whichever you feel most comfortable with.
I agree with mammy, although you could always use that high res timer dll to check, if you were that worried.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Question about Programming Logic
« Reply #3 on: July 18, 2008, 05:03:56 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I know it is not going to make that much of a difference, I was inquiring about which variation of the De Morgan logic law is best in theory towards programming.  I know they are equivalent in result, but is it correct to assume the "or" variation is essentially quicker because it checks one condition if that one condition is true?  The "or" variation has a potential to check two conditions and then returns the result, while the "and" variation always checks both conditions and then kind of negates the result in interpretation.  I am not worried about anything, in fact Game Maker is only relevant in that is what the code is being implemented into, I am just trying to get the logic down.
Logged
Re: Question about Programming Logic
« Reply #4 on: July 18, 2008, 02:32:42 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
It depends how the language works. In most optimizing compilers the or one will be faster as it won't check the second condition if the first evaluates to true. However some scripting languages or non-optimizing compilers, especially those that use stack-based evaluation, will evaluate both conditions push then pop them off the stack and then do the comparison on them which negates any form of speed increase.
« Last Edit: July 19, 2008, 11:53:34 pm by Infini »
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.195 seconds with 44 queries.