6

I decided to implement a program that can find the GCD of any two numbers (including non-integers) in TI-Basic. I've used this just fine in Java, so I know it works. It works just fine in TI-Basic, but when compared with the built-in gcd( function, it's very sluggish; the gcd( function seems to get a result in milliseconds, where mine can take several seconds. Why is TI-Basic so much slower than the predefined calculator functions?

The code


Here is the program's code in TI-Basic, for your inspection:

PROGRAM:GCD

:ClrHome
:Disp "Greatest Common","    Divisor","      ---"
:Input "First number? ",X
:Input "Second number? ",Y
:
:X→I
:Y→J
:
:If (I≠int(I) or J≠int(J))
:Then
:ClrHome
:Disp "Non-integer","inputs may be","innacurate!",""
:End
:If (I=1 or J=1)
:Then
:1→I
:1→J
:Goto Z
:End
:For(C,0,2^8)
:If I=J
:Goto Z
:
:If I>J
:I-J→I
:
:If J>I
:J-I→J
:
:End
:
:Disp "This is a hard","one! Thinking","harder..."
:
:For(C,0,2^15)
:If (I=J)
:Goto Z
:While (I>J)
:I-J→I
:C+1→C
:End
:While (J>I)
:J-I→J
:C+1→C
:End
:End
:
:Disp "TIMED OUT!","Either:",J,"or"
:Pause
:
:Lbl Z
:ClrHome
:Disp "GCD of",X,"and",Y,"is",I

Disclaimer: This is the result of me looking at my TI-84 and typing it here. Some typos might be in it, though I tried my best to keep it the same

For those of you who might not know what this means, pseudocode is provided below:

program gcd()
{
Console.clear();
Console.writeln("Greatest Common");
Console.writeln("    Divisor");
Console.writeln("      ---");

float X = Console.readFloat("First Number? ");
float Y = Console.readFloat("Second number? ");

float I = X;
float J = Y;

if (I != (int)I || J != (int)J)
{
  Console.clear();
  Console.writeln("Non-integer");
  Console.writeln("inputs may be");
  Console.writeln("inaccurate!");
  Console.writeln("");
}
if (I == 1 or J == 1)
{
  I = 1;
  J = 1;
  goto Z;
}

for(int C = 0, limit = Math.pow(2,8); C < limit; C++)
{
  if (I == J)
    goto Z;

  if (I > J)
    I = I - J;

  if (J > I)
    J = J - I;
}

Console.writeln("This is a hard");
Console.writeln("one! Thinking");
Console.writeln("harder...");

for(int C = 0, limit = Math.pow(2,15); C < limit; C++)
{
  if (I == J)
    goto z;
  while (I > J)
  {
    I = I - J;
    C++;
  }
  while (J>I)
  {
    J = J-I;
    C++;
  }
}

Console.writeln("TIMED OUT!");
Console.writeln("Either:");
Console.writeln(J);
Console.writeln("or");
Console.pause();

Z:
Console.clear();
Console.writeln("GCD of");
Console.writeln(X);
Console.writeln("and");
Console.writeln(Y);
Console.writeln("is");
Console.writeln(I);
}
dsgriffin
  • 66,495
  • 17
  • 137
  • 137
Ky -
  • 30,724
  • 51
  • 192
  • 308
  • 1
    limit=Math.pow(2,8) is happening tens, hundreds, maybe thousands of times because it is re interpreted every iteration of the loop. It's slow because your code is not as efficient as the gcd source. – Eric Leschinski Sep 24 '12 at 20:42
  • 2
    @EricLeschinski That's where you're wrong. Note that it's only called when `limit` is declared, and its return is immediately stored in `limit`. After that, the value is only read from `limit` each loop. Good eye, though! – Ky - Sep 24 '12 at 20:59
  • Oh, god, TI-Basic. This takes me back. – Mike G Jan 28 '13 at 16:57

3 Answers3

11

It's slow because it's an interpreted language - Disadvantages of interpreted languages.

Basically this affects grabbing user input, as well as displaying graphics on-screen.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • But would it really be THAT much slower? I mean, we're talking minutes versus milliseconds, here. – Ky - Sep 24 '12 at 20:44
  • 5
    Yep. See http://tibasicdev.wikidot.com/whytibasic for a TI specific explanation, and http://tibasicdev.wikidot.com/optimize-loops for information on optimizing your program (without rewriting it in assembly). – Philip Sep 24 '12 at 20:46
  • 1
    Thanks for those links! I already knew this, but didn't think it mattered *this* much. I'll keep it all in mind from now on ;D – Ky - Sep 24 '12 at 21:33
2

You could fix a lot of the code, like not using Goto, leaving parentheses off the ends of lines, not using ClrHome, etc. More info: https://en.wikibooks.org/wiki/TI-Basic_Z80_Programming/Tips,_Tricks_and_Optimizations

Zenedus
  • 96
  • 9
  • 2
    Thank you; seven years of software engineering experience has taught me things like that, and I'm glad you made note of it on this question in case future readers don't know – Ky - May 03 '19 at 15:42
1

Another reason for it being slow, is because of the clrHome, I have realized while using them, clr-home takes a moment to complete, maybe half a second, at least on the Ti-83 plus, normally disp and input don't take much, also instead of the two inputs, you could also use prompt fist number','second number'

AllAwesome497
  • 190
  • 2
  • 3
  • 14