Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about optimizing code

Author: Russell Reagan

Date: 10:22:24 07/07/03

Go up one level in this thread


On July 07, 2003 at 07:37:52, Uri Blass wrote:

>I thought about this but the problem is that in case that the loop is done more
>than once I waste time by checking case 0 again.
>
>Maybe it is the best solution because often the loop is going to be done at most
>once and I will calculate some statistic about it from the opening position.

Hi Uri,

I am pretty sure that you won't actually test anything in the switch statement.
Instead, the compiler will generate a jump table and go directly to the correct
place. For instance:

int i = some_value(); // Let's assume i = 4 now

switch (i) {
    case 0: func0(); break;
    case 1: func1(); break;
    case 2: func2(); break;
    case 3: func3(); break;
    case 4: func4(); break;
    case 5: func5(); break;
    case 6: func6(); break;
    case 7: func7(); break;
}

This code would not test cases 0, 1, 2, and 3. It would go straight to case 4
and call func4() using a jump table. I think Tim's approach would probably be
the best choice, because it is efficient and readable.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.