Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paranoia
#5
(08-13-2022, 04:51 PM)bplus Wrote: If you have sections of code doing the exact same thing, it is more efficient to put that code in a Sub or Function and call those instead of repeating code blocks, even a GOSUB is better than repeating code blocks, from "The Art of Coding". GOSUBs are a little less desirable because they aren't completely independent of the main code they are called from but sometimes they are better for convenience.

In all the preview examples, not one single block is repeated: I tried to acheve this result from the very beginning. So, in all the cases, the program lasts about 2500 lines, line plus, line minus. It is not possible, I think, to reduce the lenght or the computational burden. Generally, I use GOSUBs to make something that virtually could stay in the main code and that is virtually executed once, as a kind of "part" of the code detached. For example, if I have a section of 300 lines of calculations, I put tham in a GOSUB, instead to have these 300 lines in the main code. On the contrary, I use subroutines to do repeated things, as to to create tables and graphics. In a case, I used a subroutine to execute a kind of program in the program.

The question is purely formal.
Is it better:

Example 1:
DO
KeyPress$ = INKEY$
LOOP UNTIL KeyPress$ = "1" OR KeyPress$ = CHR$(27) OR KeyPress$ = CHR$(9)
SELECT CASE
CASE "1"
PRINT "HELLO"
CASE CHR$(27)
esc~` = 1
CASE CHR$(9)
riavvio~` = 1
END SELECT

or:

Example 2:
DO
KeyPress$ = INKEY$
LOOP UNTIL KeyPress$ = "1" OR KeyPress$ = CHR$(27) OR KeyPress$ = CHR$(9)
CASE CHR$(27)
esc~` = 1
CASE CHR$(9)
riavvio~` = 1
END SELECT

PRINT "HELLO"

(08-13-2022, 04:58 PM)SMcNeill Wrote: Generally speaking, as long as you're not dealing with looping and such, it doesn't matter.  For example:

SELECT CASE foo
  CASE 1
      'let's do lots of stuff
      'and some optional stuff that only happens when foo is one
    CASE 2
      'let's do lots of stuff like above
      'and some optional stuff that only happens when foo is two
END SELECT

Verses this:

'Let's do lots of stuff exactly as above
SELECT CASE foo
  CASE 1
      'we do the optional stuff for case 1
    CASE 2
      'we do the optional stuff for case 2
END SELECT

In both these situations, we're not going to see any real difference in program speed or performance.  We still do the bunch of stuff no matter which branch we take with the SELECT CASE, so there's no savings in performance to come along anywhere.  The second method produces less code and is generally easier to keep up with and debug, but performance wise, it's not going to be very different than the first method.

Now, where you *do* see changes in performance is when loops are involved.

FOR I = 1 to 1000
    SELECT CASE Foo
        CASE 1 'do some stuff if foo is 1
        CASE 2 'do some stuff if foo is 2
    END SELECT
NEXT

Verses the following:

SELECT CASE foo
    CASE 1
        FOR I = 1 TO !000
          'Do some stuff for when foo is 1
        NEXT
    CASE 2
        FOR I = 1 TO !000
          'Do some stuff for when foo is 2
        NEXT
END SELECT

In this situation, the second set of code will perform better than the first set of code, even though it's more code to it!!  WHY??  Because it's only making that SELECT CASE decision once, whereas the first set of code has to make that decision 1000 different times.  Structure here can play a huge part on performance, and is something to keep in mind if your code is running noticeably slower than desired.

I undestand what you say, but it is not exactly about on what I tried to explain. Let's think about my examples above in this message. Your "let's do lots of stuff", is my PRINT "HELLO", but my PRINT "HELLO" is after the DO and after (or inside) the SELECT CASE. your "let's do lots of stuff" is before the SELECT CASE and there is not DO. This changes completely the situation.

In both the Example 1 and 2, the program waits the pression of "1", "ESC" or "TAB". But in the Example 2, if "1" is pressed, the program will simply jump the SELECT CASE and it goes ahead, as in a flow, towards PRINT "HELLO". In the Example 1, if "1" is pressed, it is executed the specific CASE "1" of the SELECT CASE. We can easily think that "let's do lots of stuff" or PRINT "HELLO", as an huge part of the code where the same scheme is repeated. So, if we follow the concept of "Example 2", at the "n" interation, we actually have a kind of flow, where the SELECT CASEs are jumped while the program goes ahead, but if we follow the concept of "Example 1", at the "n" interation we are in a CASE of an nSELECT CASE, inside a CASE of a n-1SELECT CASE, inside a CASE of a n-2SELECT CASE, ...

But what is it the better between the 2? I think that at a computational level they are the same, if I am not wrong. At a logical level "Example 1" it seems to me more formally accurate, but I don't reallyt know. Both the "solutions" do the exactly the same thing, with likely the same computational effort, but they are completely different at a logical level.
Reply


Messages In This Thread
Paranoia - by bartok - 08-13-2022, 03:47 PM
RE: Paranoia - by bplus - 08-13-2022, 04:51 PM
RE: Paranoia - by SMcNeill - 08-13-2022, 04:58 PM
RE: Paranoia - by bartok - 08-13-2022, 08:59 PM
RE: Paranoia - by Kernelpanic - 08-13-2022, 08:16 PM
RE: Paranoia - by Petr - 08-13-2022, 09:16 PM
RE: Paranoia - by bartok - 08-14-2022, 07:28 AM
RE: Paranoia - by TempodiBasic - 08-15-2022, 10:04 PM
RE: Paranoia - by bartok - 08-16-2022, 10:34 AM



Users browsing this thread: 1 Guest(s)