Post Reply 
an HP question...
10-23-2017, 03:57 AM
Post: #1
an HP question...
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
Find all posts by this user
Quote this message in a reply
10-23-2017, 06:35 AM
Post: #2
RE: an HP question...
To skip all the remaining tests after a match was found...
Arno
Find all posts by this user
Quote this message in a reply
10-23-2017, 07:13 AM
Post: #3
RE: an HP question...
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...
Find all posts by this user
Quote this message in a reply
10-23-2017, 07:37 AM
Post: #4
RE: an HP question...
From the results view that is correct, but as I saud, remaining tests are skipped, that saves time.
Find all posts by this user
Quote this message in a reply
10-23-2017, 07:44 AM
Post: #5
RE: an HP question...
(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.
Find all posts by this user
Quote this message in a reply
10-23-2017, 08:06 AM
Post: #6
RE: an HP question...
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.... :-)
Find all posts by this user
Quote this message in a reply
10-23-2017, 12:16 PM (This post was last modified: 10-23-2017 12:28 PM by Gilles59.)
Post: #7
RE: an HP question...
(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.
Find all posts by this user
Quote this message in a reply
10-23-2017, 12:36 PM
Post: #8
RE: an HP question...
CASE would be useful when there is a default condition. Outside of that, I usually just use a group of IF-THEN structures myself.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-23-2017, 01:39 PM
Post: #9
RE: an HP question...
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

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
10-23-2017, 10:26 PM
Post: #10
RE: an HP question...
...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
Find all posts by this user
Quote this message in a reply
10-24-2017, 12:58 AM (This post was last modified: 10-24-2017 01:05 AM by webmasterpdx.)
Post: #11
RE: an HP question...
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
Find all posts by this user
Quote this message in a reply
10-24-2017, 01:12 AM
Post: #12
RE: an HP question...
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.
Find all posts by this user
Quote this message in a reply
10-24-2017, 01:20 AM (This post was last modified: 10-24-2017 01:39 AM by webmasterpdx.)
Post: #13
RE: an HP question...
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;
Find all posts by this user
Quote this message in a reply
10-24-2017, 01:38 AM
Post: #14
RE: an HP question...
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... :-)
Find all posts by this user
Quote this message in a reply
10-24-2017, 02:09 AM
Post: #15
RE: an HP question...
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 :-)
Find all posts by this user
Quote this message in a reply
10-24-2017, 02:16 AM (This post was last modified: 10-24-2017 02:17 AM by webmasterpdx.)
Post: #16
RE: an HP question...
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
Find all posts by this user
Quote this message in a reply
10-24-2017, 06:01 AM
Post: #17
RE: an HP question...
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

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
10-24-2017, 06:21 AM
Post: #18
RE: an HP question...
(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.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)