A game that actually hits the limits

Started by SSH, Wed 23/07/2008 22:52:26

Previous topic - Next topic

Kweepa

#20
There are two standard ways to optimize a large switch statement.
1) For a mostly contiguous block of numbers (eg run-script numbers such as 1, 2, 5, 6, 7, 8, 10, 12, 13, 14) a compiler would generate a jump table (with null table entries for the missing numbers). The execution time of the code this produces is independent of the number of cases, i.e. it's O(1). Unfortunately this isn't available from AGS script.
2) For sparse distributions of numbers, the compiler makes up a binary search to the appropriate value, which would look something like this (here with contiguous numbers):

Code: ags

if (p < 10)
{
  if (p < 5)
  {
    if (p < 3)
    {
      if (p == 1) Do(1); else Do(2);
    }
    else
    {
      if (p == 3) Do(3); else Do(4);
    }
  }
  else
  {
    // do 5-9 as above
  }
}
else
{
  // do 10-20
}

This code executes in a time dependent on log(number of cases), i.e. O(lnN).

You could write a C# script that would make the shell of this code for you and then you'd just have to fill in the implementation of the various cases.
Still waiting for Purity of the Surf II

SMF spam blocked by CleanTalk