Jump to content



0

Which is faster?


15 replies to this topic

#1 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,923 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Fri Jul 29, 2011 9:52 PM

Which is faster?


This:

temp6 = ((player0y-4)/4)-1



Or this:

temp6 = player0y-4 : temp6 = temp6/4 : temp6 = temp6-1



Or is there an even faster way than either of those?



Thanks.

#2 jwierer OFFLINE  

jwierer

    Dragonstomper

  • 746 posts
  • Location:Seattle,WA

Posted Fri Jul 29, 2011 9:57 PM

View PostRandom Terrain, on Fri Jul 29, 2011 9:52 PM, said:

Which is faster?


This:

temp6 = ((player0y-4)/4)-1



Or this:

temp6 = player0y-4 : temp6 = temp6/4 : temp6 = temp6-1



Or is there an even faster way than either of those?



Thanks.
You could simplify the math. ((player0y-4)/4)-1 = (player0y/4 - 4/4) -1 = player0y/4 - 2

temp6 = (player0y/4)-2

-Jeff

#3 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,923 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Fri Jul 29, 2011 10:04 PM

View Postjwierer, on Fri Jul 29, 2011 9:57 PM, said:

You could simplify the math. ((player0y-4)/4)-1 = (player0y/4 - 4/4) -1 = player0y/4 - 2

temp6 = (player0y/4)-2
Thanks. That's an improvement, but in cases where you can't do something simple like that, I wonder which style would be faster?

#4 ScumSoft OFFLINE  

ScumSoft

    Moonsweeper

  • 331 posts
  • Location:Polysorbate 60

Posted Fri Jul 29, 2011 10:07 PM

You can always do something simple like that.
There are no existing cases where this math cannot be applied on the 2600 :)

 asm
 lda player0y ;[0]+3
 lsr          ;[3]+2
 lsr          ;[5]+2
 sec          ;[7]+2
 sbc #2       ;[9]+2
 sta temp6    ;[11]+3 14-cycles for routine
end

You would be trying to optimize this routine, which is already pretty much as optimized as you can get.

Edited by ScumSoft, Fri Jul 29, 2011 10:08 PM.


#5 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,923 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Fri Jul 29, 2011 10:24 PM

View PostScumSoft, on Fri Jul 29, 2011 10:07 PM, said:

You can always do something simple like that.
I don't know what you mean. For example, if it was temp6 = ((player0y-3)/4)-1 instead, you couldn't do what jwierer did. He lucked out because the number I was subtracting turned out to be 4, but at one time, the number was something else.

#6 RevEng OFFLINE  

RevEng

    River Patroller

  • 2,010 posts
  • bit shoveler
  • Location:Canada

Posted Fri Jul 29, 2011 11:02 PM

View PostRandom Terrain, on Fri Jul 29, 2011 9:52 PM, said:

Which is faster?

temp6 = ((player0y-4)/4)-1
...takes 18 cycles

temp6 = player0y-4 : temp6 = temp6/4 : temp6 = temp6-1
...takes 26 cycles.

When bB works though a complex statement, it uses the registers (faster) instead of memory locations (slower) to hold the intermediate calculations.

There's not a faster way to do it in bB, with the exception of assembly or simplifying the math, as demonstrated.

#7 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,923 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Fri Jul 29, 2011 11:07 PM

View PostRevEng, on Fri Jul 29, 2011 11:02 PM, said:

View PostRandom Terrain, on Fri Jul 29, 2011 9:52 PM, said:

Which is faster?

temp6 = ((player0y-4)/4)-1
...takes 18 cycles

temp6 = player0y-4 : temp6 = temp6/4 : temp6 = temp6-1
...takes 26 cycles.

When bB works though a complex statement, it uses the registers (faster) instead of memory locations (slower) to hold the intermediate calculations.

There's not a faster way in bB, with the exception of assembly or simplifying the math, as demonstrated.
Thanks. That's good to know.

#8 ScumSoft OFFLINE  

ScumSoft

    Moonsweeper

  • 331 posts
  • Location:Polysorbate 60

Posted Sat Jul 30, 2011 12:08 AM

View PostRandom Terrain, on Fri Jul 29, 2011 10:24 PM, said:

View PostScumSoft, on Fri Jul 29, 2011 10:07 PM, said:

You can always do something simple like that.
I don't know what you mean. For example, if it was temp6 = ((player0y-3)/4)-1 instead, you couldn't do what jwierer did. He lucked out because the number I was subtracting turned out to be 4, but at one time, the number was something else.
What I meant was that you can always break down something complex and make it simple for the 6502.
((player0y-3)/4)-1 is the same as:

 rem divide ((a-b)/4) - 1
 asm
 lda temp1  ;Load any value 0-255 in A
 sec        ;Set carry
 sbc temp2  ;Subtract any value 0-255
 lsr        ;Divide by 2
 lsr        ;Divide by 4
 clc        ;Clear carry
 sbc #1     ;Subtract 1 from result
 sta temp3  ;Store result
end

This is just a straight forward method, I am sure math tricks can be used to do the same in less cycles.

#9 bogax OFFLINE  

bogax

    Star Raider

  • 61 posts

Posted Mon Aug 1, 2011 12:40 AM

View PostRandom Terrain, on Fri Jul 29, 2011 10:24 PM, said:

I don't know what you mean. For example, if it was temp6 = ((player0y-3)/4)-1 instead, you couldn't do what jwierer did. He lucked out because the number I was subtracting turned out to be 4, but at one time, the number was something else.

temp6 = (player0y-7)/4

#10 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,923 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Aug 1, 2011 3:44 AM

View Postbogax, on Mon Aug 1, 2011 12:40 AM, said:

View PostRandom Terrain, on Fri Jul 29, 2011 10:24 PM, said:

I don't know what you mean. For example, if it was temp6 = ((player0y-3)/4)-1 instead, you couldn't do what jwierer did. He lucked out because the number I was subtracting turned out to be 4, but at one time, the number was something else.

temp6 = (player0y-7)/4
That's what I get for pulling a random number out of the air. Now try temp6 = ((player0y-6)/4)-1. I'll figure out how you do that magic trick sooner or later.

:D

#11 GroovyBee OFFLINE  

GroovyBee

    7800 Developer

  • 5,781 posts
  • Busy bee!
  • Location:North, England

Posted Mon Aug 1, 2011 3:47 AM

temp6=(player0y-10)/4 :P

#12 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,923 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Aug 1, 2011 3:51 AM

View PostGroovyBee, on Mon Aug 1, 2011 3:47 AM, said:

temp6=(player0y-10)/4 :P
Damn you math magicians! I'll never figure out your trick! Where do you keep the rabbit? You aren't wearing any sleeves!

:D

#13 GroovyBee OFFLINE  

GroovyBee

    7800 Developer

  • 5,781 posts
  • Busy bee!
  • Location:North, England

Posted Mon Aug 1, 2011 4:25 AM

Its easy to see the answer when you write it as :-

temp6=((player0y-6)/4)-1
temp6=((player0y-6)/4)-(4/4)
temp6=(player0y-6-4)/4
temp6=(player0y-10)/4

#14 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,923 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Aug 1, 2011 5:04 AM

View PostGroovyBee, on Mon Aug 1, 2011 4:25 AM, said:

Its easy to see the answer when you write it as :-

temp6=((player0y-6)/4)-1
temp6=((player0y-6)/4)-(4/4)
temp6=(player0y-6-4)/4
temp6=(player0y-10)/4
Even after seeing where you guys were hiding the rabbit, it still looks like magic. Very weird, but cool. Thanks.

#15 bogax OFFLINE  

bogax

    Star Raider

  • 61 posts

Posted Mon Aug 1, 2011 11:21 AM

View PostRandom Terrain, on Mon Aug 1, 2011 5:04 AM, said:

Even after seeing where you guys were hiding the rabbit, it still looks like magic. Very weird, but cool. Thanks.

Not sure how to answer that.

My gut reaction is to say there's no magic to it.

But it is sort of magical or wonderous or at least fascintaing,
maybe that's the key to interest.

What it is is pretty mundane 7th grade algebra.

you wouldn't have any trouble learning it.

Boolean algebra has similar rules/properties

#16 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,923 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Aug 1, 2011 11:35 AM

I took algebra in 10th and 11th grade, but I forgot pretty much everything I learned.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users