0

When coding on a very slow machine, such as a TI-89, every statement counts.

I am often indecisive between two ways of writing if statements:

If bool Then
    int = 0
Else
    int = 1
End

or

int = 1
If bool Then
    int = 0
End

(Yes, I know with this example it doesn't really matter)

So the question is, which one is faster and why?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
epicbob57
  • 51
  • 8
  • BENCHMARK sigh....... can avoid the branch using an XOR .... – Mitch Wheat Jan 11 '17 at 05:01
  • If only there were some method we could use to find this out. I think, were I to invent it, I might call it "benchmarking". :-) – paxdiablo Jan 11 '17 at 05:06
  • Usually the later is better because affecting a value to a variable is a lot faster than two potential jumps. "Branching is bad for performance". Now, it really depends on how the language works. A TI-89 certainly uses a very slow interpreter, in which case, the less characters the better. – Guillaume F. Jan 11 '17 at 06:07
  • @MitchWheat I'm also curious about the _why_ – epicbob57 Jan 11 '17 at 15:13
  • 2
    The TI-89 uses a Motorola 68k. GCC can compile into 68k. I never tested for this particular architecture, but I am willing to bet that writing C code and having the compiler worry about optimization is faster than TI-BASIC. Also you get to write in C :-) – FirefoxMetzger Jan 16 '17 at 10:08
  • 1
    Yeah, @FirefoxMetzger , there is actually a program called TIGCC for that! I do use it, but there are occasions when I need on-calc programming. – epicbob57 Jan 17 '17 at 15:32

1 Answers1

1

For TI-89 Basic, use when: when(bool,0,1).

bb94
  • 1,294
  • 1
  • 11
  • 24