Computed gotos on Reddit

Walter Bright newshound2 at digitalmars.com
Thu Jul 26 13:38:13 PDT 2012


On 7/26/2012 8:55 AM, Dmitry Olshansky wrote:
>> int test(int i)
>> {
>>      switch (i)
>>      {
>>          case 3: i += 3; break;
>>          case 4: i += 4; break;
>>          case 5: i += 5; break;
>>          case 6: i += 6; break;
>>          case 7: i += 7; break;
>>          case 8: i += 8; break;
>>          default: i += 100; break;
>>      }
>>      return i;
>> }
>>
>
> Do the above in loop. And more cases of course. Something around 40 should do.


Here's my entire test program. It runs a consistent 5 to 10% slower with the new 
method compared with the old. Color me very disappointed.
=======================================================
import core.stdc.stdio;

int test(int i)
{
     switch (i)
     {
         case 3: i += 3; break;
         case 4: i += 4; break;
         case 5: i += 5; break;
         case 6: i += 6; break;
         case 7: i += 7; break;
         case 8: i += 8; break;
         default: i += 100; break;
     }
     return i;
}

void main()
{
     for (int i = 0; i < 100000000; i++)
     {
         for (int j = 0; j < 10; j++)
             test(j);
     }
     printf("%d\n", test(6));
}


More information about the Digitalmars-d mailing list