Forum > General

Did you know this cool if-then-else syntax ?

<< < (2/3) > >>

MathMan:

--- Quote from: howardpc on November 28, 2014, 09:42:39 am ---
--- Quote from: MathMan on November 28, 2014, 01:45:25 am ---Does that answer your question?

--- End quote ---

Yes, if you are saying that optimisation of case statements is related only to the previous case selection history and the relative likelihoods of any future selections; and that the physical ordering of case variables  in the source (e. g. by ordinal priority) is immaterial.

--- End quote ---

Yes - it looks like it. Though I haven't analysed case statements with multiple selections yet but in case of the analysed boolean one your summary is spot on.

MathMan:

--- Quote from: howardpc on November 28, 2014, 09:42:39 am ---
--- Quote from: MathMan on November 28, 2014, 01:45:25 am ---Does that answer your question?

--- End quote ---

Yes, if you are saying that optimisation of case statements is related only to the previous case selection history and the relative likelihoods of any future selections; and that the physical ordering of case variables  in the source (e. g. by ordinal priority) is immaterial.

--- End quote ---

I am awfully sorry but I have to modify my previous statement a bit <sigh>. My only excuse is that it was late yesterday and I had forgotten that my project settings disabled optimization (had set it to O0). Did the analysis again though with O3 enabled and also looked into with a fan-out of 4 or 8 selections to understand whats happening there.

The compiler still generates the same code, regardless of how you order the cases - but the code itself is a bit different. However the suggestion I made regarding the boolean case still holds. For cases with a fan-out of 4 I analysed


--- Code: ---  case ByteVar of
    0: DoNull();
    1: DoOne();
    2: DoTwo();
    3: DoThree();
  end;

--- End code ---

Regardless of the ordering in the case statement the compiler generates code that first tests for 0 and branches accordingly, then 1, then 2 and finally 3. My advise in this case would be to associate the case with most probability to the lowest numeric selection - if this can be done easily. That is if you have to do substantial pre-computation to get to this association simply stick with what you have.

The same holds for fan-out 8 where the chain of branches is simply extended accordingly by the compiler - but still proceeding from low to high.

Regards,
MathMan

marcov:

Obfuscated pascal contests are always fun!


--- Code: ---
const falsetrue:array[boolean] of string =('okiqjksdf5er','54sdfgikeks');

[program Project1;
begin
  randomize;
  case random(77) > 44 of
    true: writeln('mlsdfg54f');
    false: writeln(falsetrue[random(43) < 12]);
  end;
  readln;
end.
[code]
--- End code ---

Bart:
This needs IsTrue() and IsFalse() from BoolFactory unit  >:D

Bart

Ñuño_Martínez:
I've found that "CASE" stuff instead "IF" but, after explanation by MathMan I still not see the difference.  I mean, it explains two different "CASE"  but not difference with "IF"...

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version