HP Forums
an HP question... - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: an HP question... (/thread-9351.html)



an HP question... - webmasterpdx - 10-23-2017 03:57 AM

Why does the case command exist since we have to put an if-then-end command for each case? Why not just put the if-then-end commands in a row without the case?
just curious...
Thx
-Donald


RE: an HP question... - Arno K - 10-23-2017 06:35 AM

To skip all the remaining tests after a match was found...
Arno


RE: an HP question... - webmasterpdx - 10-23-2017 07:13 AM

what remaining tests?
e.g. you can have:

if (x==1) then ... return; end;
if (x==2) then ... return; end;
if (x==3) then ... return; end;
...default...
return; end;

or

case:
if (x==1) then ... end;
if (x==2) then ... end;
if (x==3) then ... end;
default: ...
end;
return;

I mean, it makes the default statement easier, but that's about it...


RE: an HP question... - Arno K - 10-23-2017 07:37 AM

From the results view that is correct, but as I saud, remaining tests are skipped, that saves time.


RE: an HP question... - Didier Lachieze - 10-23-2017 07:44 AM

(10-23-2017 07:13 AM)webmasterpdx Wrote:  what remaining tests?
e.g. you can have:

if (x==1) then ... return; end;
if (x==2) then ... return; end;
if (x==3) then ... return; end;
...default...
return; end;

This is assuming the case statement is the last one before the return.
If you have some instructions after the case:

case:
if (x==1) then ... end;
if (x==2) then ... end;
if (x==3) then ... end;
default: ...
end;
<some more instructions>
return;

then if x==2 the execution will go directly from the end of the if (x==2) sequence to <some more instructions>, skipping the remaining if and default.


RE: an HP question... - webmasterpdx - 10-23-2017 08:06 AM

I'm more interested into what it compiles to. I wonder what would just the if statements with break; instead of return there would do....I'll have to try it some time to see if it'd work first of all, and then try a speed test.... :-)


RE: an HP question... - Gilles59 - 10-23-2017 12:16 PM

(10-23-2017 03:57 AM)webmasterpdx Wrote:  Why does the case command exist since we have to put an if-then-end command for each case? Why not just put the if-then-end commands in a row without the case?
just curious...
Thx
-Donald


I dont like the way CASE is implemented in the Prime.
In langage like Pascal it's easier to understand and read, but perhaps less flexible.

Code:
Case x Of
 1 : do something;
 2 : do someting else ;
 3 : etc;
 Default; ...
End;

or

Code:
Case x Of
 1,2,9: do domething if x=1 ou or x=2 or x=9;
 3..7 : do someting else if x between 3 and 7 ;
 8    : etc;
 Default;
End;

By the way, the CASE instruction is interesting on the PRIME as Didier Lachieze wrote and avoid a succession of imbricated IF THEN ELSE. And if understand well your exemple with RETURN only works if you have a subroutine dedicated to the test cases.


RE: an HP question... - Eddie W. Shore - 10-23-2017 12:36 PM

CASE would be useful when there is a default condition. Outside of that, I usually just use a group of IF-THEN structures myself.


RE: an HP question... - cyrille de brébisson - 10-23-2017 01:39 PM

Hello,

CASE exists because it is a "normal" "expected" programming command which is taught in algo classes and since Prime is an educational tool, it has been implemented.

It is not compiled as an if then else, but as a case function whith it's own handling. It does offer a "default"...

Personally, unless I know that case compiles to a jump table (as in a switch (a) where a is known between 0 and 15 (for example), then I will NEVER use case.

Cyrille


RE: an HP question... - webmasterpdx - 10-23-2017 10:26 PM

...like the switch statement in C. I'd have to agree....it kind of took me by surprise that it wasn't like that...
switch(v)
{
case 1:
...
break;
case 2:
...
break;
default:
break;
}

However, for the default part, it's worth it.
-D


RE: an HP question... - webmasterpdx - 10-24-2017 12:58 AM

I did some timing using TEVAL, and got some unusual results.
I used the following program:
Code:

EXPORT T1(V,N) // Using alternate to CASE
BEGIN
FOR I FROM 1 TO N DO
REPEAT
 IF (V==1) THEN C:=2; BREAK; END;
 IF (V==2) THEN C:=3; BREAK; END;
 IF (V==3) THEN C:=5; BREAK; END;
 IF (V==4) THEN C:=7; BREAK; END;
 C:=0; // DEFAULT
UNTIL 1; // REPEAT...ALWAYS
END; // FOR
END; // BEGIN

EXPORT T2(V,N) // Using CASE statement
BEGIN
FOR I FROM 1 TO N DO
CASE
 IF (V==1) THEN C:=2; END;
 IF (V==2) THEN C:=3; END;
 IF (V==3) THEN C:=5; END;
 IF (V==4) THEN C:=7; END;
DEFAULT:
 C:=0;
END; // CASE
END; // FOR
END; // BEGIN

EXPORT TEST(V,N) // Timing CASE Vs Alternative
BEGIN
LOCAL A,B;
A:=TEVAL(
T1(V,N)
); // TEVAL
B:=TEVAL(
T2(V,N)
);
RETURN {A,B,C};
END; // BEGIN

The results are:
TEST(1,1) gives {0_s,0.001_s,2}
This indicates the CASE alternative is faster on a single run, but...
TEST(1,1000) gives {0.086_s,0.069_s,2}
This indicates that with a loop of 1000 times, the CASE statement is faster????

In general, TEST(n,1000) indicates that the CASE statement is faster, even for when the DEFAULT is used.

I notice that TEVAL can give different values on successive runs. Why is that?

Also, why would the CASE alternative seem to give a faster run time than CASE for a run of 1, but as soon as N (the loop count) rises, typically with a value of 2, the CASE statement is faster????

Thanks
-Donald


RE: an HP question... - webmasterpdx - 10-24-2017 01:12 AM

Note that I think the reason the CASE alternative looks like it's faster is that the measurement of time isn't that accurate and I've seen it alternate between which method is faster with only one run....so it's just an accuracy issue, but the 1000 loop count gives us the real result, which is that the CASE statement is faster.

I also tried it with CONTINUE instead of BREAK, with the same result.


RE: an HP question... - webmasterpdx - 10-24-2017 01:20 AM

I just edited this post....it's in error....everything below this is Rubbish....my WHILE 0 stops the code from ever reaching the IF statements. Leaving this here as a lesson....WHILE 0 never executes whats inside it... LOL

I got some bad news....I found an alternative to CASE that is considerably faster to execute...
I just used the code above, but instead of REPEAT....UNTIL 1; use WHILE 0 DO.....END;
I get results like {0.018_s,0.064_s,2} for example!!!!

That's about 3 times faster....so the faster CASE statement is to do this:

Code:

// Start of CASE statement
WHILE 0 DO
 IF ... THEN ... BREAK; END;
 IF ... THEN ... BREAK; END;
 ... // DEFAULT statement
END;



RE: an HP question... - webmasterpdx - 10-24-2017 01:38 AM

Never mind....I'm wrong....while 0 never goes through the loop...it never checks...LOL!
No wonder it's faster...it never reaches any of the IF statements... :-)


RE: an HP question... - webmasterpdx - 10-24-2017 02:09 AM

I tried WHILE 1, For J FROM 1 TO 2 DO and REPEAT...UNTIL 1, and they are all slower than CASE.

Even eliminating the inner loop altogether and using CONTINUE, CASE is still faster as the CONTINUE evaluation slows it enough to be slower than the CASE statement.

So, yeah, I'm good with CASE now :-)


RE: an HP question... - webmasterpdx - 10-24-2017 02:16 AM

I know why TEVAL is not consistent with small measurements. The LCD refresh interrupt and other timer interrupts (like battery check, etc)....all make small TEVAL measurements inconsistent. You need to put your measurement in a loop to put it at around 1 second to get accurate measurements.

Also, you cannot assign a variable like the output of TEVAL to a global variable like C, as 2.5_s isn't a real....i.e. A value with units isn't accepted by HOME.

So, I couldn't run A:=TEST(1,1000) from Home mode.


-Donald


RE: an HP question... - cyrille de brébisson - 10-24-2017 06:01 AM

Hello,

The OS scheduler is 1ms.
so all timing mesurement is to plus or minus 1ms.
They are a LOT of threads in the system that might take control of the system (temorary) do do stuff.
So any time mesurement smaller than 100ms are to be taken with a grain of salt because lot of other things might have slow things down.

Cyrille


RE: an HP question... - Joe Horn - 10-24-2017 06:21 AM

(10-24-2017 02:16 AM)webmasterpdx Wrote:  Also, you cannot assign a variable like the output of TEVAL to a global variable like C, as 2.5_s isn't a real....i.e. A value with units isn't accepted by HOME.

So, I couldn't run A:=TEST(1,1000) from Home mode.

Try using time(event) instead of teval(event). It returns the same timing but without a unit attached. Just be sure to spell it with all lowercase letters, or it won't work. Bonus: Unlike teval(), time() works in CAS too.

Disclaimer: It's an undocumented function, so it might vanish without warning in a future firmware upgrade.