Author Topic: Virtuous programming techniques...  (Read 2913 times)

0 Members and 1 Guest are viewing this topic.

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Virtuous programming techniques...
« on: November 22, 2020, 12:44:39 pm »
Hello everyone, wherever you are!

A general question:

How much does the use of CALL affect the use of resources (CPU, memory and more) in terms of resource exploitation?
For example, does the massive use of '$ INCLUDE instead of CALL damage system and program performance?
And the use of COMMON SHARED or DIM SHARED, in the same way, damages them?

Thank you!

« Last Edit: November 22, 2020, 12:47:33 pm by krovit »
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Virtuous programming techniques...
« Reply #1 on: November 23, 2020, 12:28:44 am »
A literal include will take up more RAM as a CALL allows you to reuse the same code by jumping to it and returning to where you were.  Since it takes some time to jump to it, it will run a bit slower.  The same goes for LOOPs, literally typing out the 'unrolled' loop will run faster. Similar goes for SHARED vs. parameter passing, memory used up by parameters is freed to be reused after the CALL but SHARED will keep it occupied the entire program run time but might make your sub run faster.

It's up to the programmer to cleverly decide when he should save memory at the expense of speed or vice versa. I would say that in general, unrolling CALLs and LOOPs should be avoided unless you are really sure that the performance gain isn't trivial

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Virtuous programming techniques...
« Reply #2 on: November 23, 2020, 03:00:06 am »
Minimizing memory usage seems rather pointless, most of the time.  It was an
issue with QB4.5, where one could run out of it a lot easier. 

Some tricks for improving speed are:

1) Use LONG variables.  They're the fastest.  DEFLNG A-Z at the start of the code defaults
   all vars LONG unless specifically typed otherwise.  Why LONG is faster than INTEGER I
   don't understand.
2) DO and WHILE loops are faster than FOR:NEXT
3) branchless programming:
   IF var1 > 5 THEN var2 = 77 ELSE var2 = 0
     can be replaced with
   var2 = (var1 > 5) * -77
4) Avoid strings in loops that are critical to program performance.  String processing
   is hellishly slow.

I don't know if old tricks like using lookup tables for trigonometric functions matter
with QB64 and modern computers.  But it's easy enough to test. 

For most programs, QB64 is so fast that the issue is to SLOW IT DOWN, not speed it up.
Sprinkling in a few _LIMITs and/or _DELAYs prevents old QB4.5 code from transforming the
CPU into a toaster oven.


It works better if you plug it in.

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: Virtuous programming techniques...
« Reply #3 on: November 23, 2020, 06:35:27 pm »
Thank you... in general I imagined something like that but you have given me precise and tranquilizers indications.

Quote
3) branchless programming:
   IF var1 > 5 THEN var2 = 77 ELSE var2 = 0
     can be replaced with
   var2 = (var1 > 5) * -77
I didn't know the ability to compose code that way !!

Strange this thing about the LONG variables... I, by default, always defined them all INTEGER thinking it was the best thing.


Quote
I would say that in general, unrolling CALLs and LOOPs should be avoided unless you are really sure that the performance gain isn't trivial

Too many CALLS could be a problem? Damn it! Certain programs live from calls to CALL routines.


Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Virtuous programming techniques...
« Reply #4 on: November 23, 2020, 10:50:32 pm »
Too many CALLS could be a problem? Damn it! Certain programs live from calls to CALL routines.

I meant the opposite

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: Virtuous programming techniques...
« Reply #5 on: November 24, 2020, 09:24:56 am »
__vince, thank you!

the world had collapsed on me!

you do it to say... but when certainties falter (like call is a blessing...) you get shaken...

Sorry... i'm bad in foreign languages
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)