Does an 'If' inside a macro get compiled-in if false?

Just starting out? Need help? Post your questions and find answers here.
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

Does an 'If' inside a macro get compiled-in if false?

Post by OgreVorbis »

For example (pseudo code)...

Code: Select all

Macro DrawClock(x, y, hand, hand2, hand3, enableThing)
	Line(...)
	Line(...)
	If enableThing
		Line(...)
		Line(...)
		More stuff...
	EndIf
EndMacro
Does that produce the code with the If statement compiled in, but it always skips if enableThing is false, or is the if statement not even compiled? Like it never existed?

I have a design question also. I'm writing a program that draws a clock on the desktop (fully animated analog) and as such, it needs to consume as little CPU as possible. I want to make sure it does not send the CPU into a high power state, so far it's good. There will be several different options for clocks. The most comprehensive way to make it would be to have an array of structures, one for each clock, and then pass that specific array element to a DrawClock Procedure. This seems nice code-wise, but for efficiency, I thought using a Macro for drawing the clock inside of a switch case for each different clock might be more efficient. What do you think?
My blog/software site: http://dosaidsoft.com/
User avatar
jacdelad
Addict
Addict
Posts: 1477
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Does an 'If' inside a macro get compiled-in if false?

Post by jacdelad »

The macro has no effect on the power consumption of your program. The code inside the macro is placed at the point where the macro is called. Think of it more as a way to slim down your code instead of writing the drawing routine for each click separately. Plus you don't have to change each one of them if the code is updated.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

Re: Does an 'If' inside a macro get compiled-in if false?

Post by OgreVorbis »

jacdelad wrote: Thu Nov 18, 2021 4:52 pm The macro has no effect on the power consumption of your program. The code inside the macro is placed at the point where the macro is called. Think of it more as a way to slim down your code instead of writing the drawing routine for each click separately. Plus you don't have to change each one of them if the code is updated.
Yeah, I know how it works. I'm just saying that if I use a procedure instead, it has to use the stack and structures and maybe other things internally.
So I think the macro would very slightly reduce the CPU cycles of the program (over a proc with a struct).

The main question has not been answered though. If you don't know, that's cool, I'm just pointing that out if someone wants to answer.
My blog/software site: http://dosaidsoft.com/
User avatar
STARGÅTE
Addict
Addict
Posts: 2087
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Does an 'If' inside a macro get compiled-in if false?

Post by STARGÅTE »

OgreVorbis wrote: Thu Nov 18, 2021 4:22 pm Does that produce the code with the If statement compiled in, but it always skips if enableThing is false, or is the if statement not even compiled? Like it never existed?
The macro inserts its content at the position of its call, while all parameters are replaced.
This means, the If-block is always compiled and "enableThing" is replaced by the parameter you passed during the macro call.
If you pass #False, the If-block is compiled anyway, because If is a runtime keyword.
It would result in "If #False" and the code is compiled but not executed.

When you write a macro like:

Code: Select all

Macro DrawClock(x, y, hand, hand2, hand3, enableThing)
	Line(...)
	Line(...)
	CompilerIf enableThing
		Line(...)
		Line(...)
		More stuff...
	CompilerEndIf
EndMacro
then the code inside the CompilerIf block is only compiled, when enableThing is #True during the compilation.

Two the second question: "I thought using a Macro for drawing the clock inside of a switch case for each different clock might be more efficient. What do you think?"
No. All drawing commands are much more time consuming than a little procedure call. Macros would not help.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Does an 'If' inside a macro get compiled-in if false?

Post by mk-soft »

You should work with structures and create a separate memory area for each clock.

Or immediately as OOP :wink:

Link: Own Flat Gadgets as Object
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

Re: Does an 'If' inside a macro get compiled-in if false?

Post by OgreVorbis »

That answered the question perfectly. Thanks.
And I didn't think about the ratio of time spent in drawing vs. my procedures and stuff. Clearly the drawing takes the longest especially because I'm using an anti-aliasing module. (Oh, and I know PB has AA built in, just not in GDI basic 2D graphics mode.)
My blog/software site: http://dosaidsoft.com/
Post Reply