Page 1 of 2
Do Constants help speed up your Program ?
Posted: Tue Sep 24, 2019 8:49 pm
by VB6_to_PBx
Do #Constants i create , help speed up my .EXE Program ?
any differences in .EXE speed , with or without using #Constants i create ???
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 12:44 am
by skywalk
What are you asking
Use #MyConst = 42 instead of MyVar = 42?
This seems quite trivial.
Do you mean #MyGadgetNbr vs #PB_Any?
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 3:02 am
by VB6_to_PBx
skywalk wrote:What are you asking
Use #MyConst = 42 instead of MyVar = 42?
This seems quite trivial.
Do you mean #MyGadgetNbr vs #PB_Any?
i mean using #MyConst = 42 -vs- only the number 42 , as the Gadget's number
is there anyway to test this with a short PureBasic Code in a Loop ? , maybe like 50,000 Loops or something
call a Procedure with a Gadget that has #MyConst = 42 -vs- only the number 42 ( or even same test -vs- #PB_Any )
anyone ever tested this before ?
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 5:29 am
by skywalk
The compiler converts literals into constants.
What is happening with you?
Why do you need speed tests for a gadget reference?
Did you run the profiler to see your most used code?
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 6:02 am
by Shield
There's no difference between using a constant and using the literal value.
PB literally replaces all constants with their values wherever they occur.
However, using constants for Gadgets instead of #PB_Any will waste space if you leave gaps
between the IDs. For example, if you create a gadget with ID 50000, PB will create an array
that holds 50000 elements, even if you don't use them (assuming this implementation hasn't changed over the years).
But, as skywalk correctly pointed out, what are you doing, mate?
Forget about possible performance implications that simply don't matter at all and are in no way detectable.
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 6:52 am
by VB6_to_PBx
skywalk wrote:The compiler converts literals into constants.
What is happening with you?
Why do you need speed tests for a gadget reference?
Did you run the profiler to see your most used code?
i happened to change/replace some Gadget #Constants to just Numbers
and re-ran my Program and seemed to notice a just decernible lag in Program startup
i retested more times changing Gadget #Constants -vs- Numbers and i cannot tell a difference in startup time
i'm on WIN10 Computer , so i think what really happened was WIN Update going on or Virus Scan came on or Virus Protection update
as i no longer see a time difference .
since my Program is going to end up pretty large or huge with Gadgets , when it happened the very 1st time i became a little worried
that some of the Gadget #Constants i changed to Numbers caused a Lag , but now no worries !

... it appears there's no difference in speed .
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 7:00 am
by Joubarbe
Clearly you wouldn't "feel" any difference. Anyway, keep in mind that constants don't take any place into memory. They act like macros and are replaced at compile time. So, in that regard, we could say that it's faster, but you really shouldn't ask yourself this question

Constants should however be used to make your code more readable. You wouldn't see any difference between 10k constants and 10k variables.
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 7:07 am
by VB6_to_PBx
Joubarbe wrote:Clearly you wouldn't "feel" any difference.
Anyway, keep in mind that constants don't take any place into memory.
They act like macros and are replaced at compile time. So, in that regard, we could say that it's faster, but you really shouldn't ask yourself this question
Constants should however be used to make your code more readable. You wouldn't see any difference between 10k constants and 10k variables.
thanks Joubarbe , for this Info !
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 12:36 pm
by juergenkulow
Hello VB6_to_PBx,
please find the following codes:
Code: Select all
Declare MyProc()
; x64-ASM-Code from Commandline> pbcompiler myvar.pb --commented
#MyConst=42
MyVar.q=42 ; MOV qword [v_MyVar],42
Value=#MyConst ; MOV qword [v_Value],42
Value=MyVar ; MOV rax,qword [v_MyVar] : MOV qword [v_Value],rax
MyProc() ; CALL _Procedure0
Procedure MyProc()
Protected MyVarP.q=42 ; MOV qword [rsp+40],42
Protected ValueP=#MyConst ; MOV qword [rsp+48],42
ValueP=MyVarP ; PUSH qword [rsp+40]
; POP rax
; MOV qword [rsp+48],rax
EndProcedure
Code: Select all
; PB 5.71 x64 Windows
EnableExplicit
Structure EAXEDXTyp : StructureUnion : quadel.q : longel.l[1] : EndStructureUnion : EndStructure
Macro how_long(Code,Result) ; Must be called in a Procedure.
DisableDebugger
EnableASM
CompilerIf Not Defined(StartRDTSC,#PB_Variable)
Protected StartRDTSC.EAXEDXTyp, StopRDTSC.EAXEDXTyp
CompilerEndIf
RDTSC ; Write CPU-Time in eax and edx register
MOV StartRDTSC\longel[0],eax
MOV StartRDTSC\longel[1],edx
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code ; 100x Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
Code : Code :Code :Code :Code : Code :Code :Code :Code :Code
RDTSC
MOV StopRDTSC\longel[0],eax
MOV StopRDTSC\longel[1],edx
Result=StopRDTSC\quadel-StartRDTSC\quadel
DisableASM
EnableDebugger
EndMacro
Procedure Main3()
#MyConst=42
Protected Messwert, Messwert2, Messwert3, Value, MyVar.q
how_long(Value=#MyConst ,Messwert)
how_long(MyVar=42, Messwert2)
how_long(Value=MyVar,Messwert3)
Protected s.s="Time Const to Var:"+Str(Messwert)+#CRLF$
s+"Time Var to Var:"+Str(Messwert2+Messwert3)
SetClipboardText(s)
MessageRequester("within RDTSC",s)
EndProcedure
Main3()
; Time Const To Var:480
; Time Var To Var:1161
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 5:08 pm
by VB6_to_PBx
hi juergenkulow ,
Thank you very much for creating this Test !
on new cheap/sale item HP570-p054 WIN10 64-Bit Computer with PB5.41LTS 32-Bit Compiler i get :
with Debugger On :
Time Const to Var:4411
Time Var to Var:7781
Const to Var = almost twice as fast
with Debugger Off :
Time Const to Var:103
Time Var to Var:892
Const to Var = a bunch faster
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 7:24 pm
by skywalk
What the heck are you comparing exactly?
Explain it like I am 5 years old.
Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 8:29 pm
by Josh
skywalk wrote:What the heck are you comparing exactly?
Explain it like I am 5 years old.
It's simple. It is found that
Literal > Variable.i
is faster than
Literal > Variable.q > Variable.i
I think I have to be a genius because I wouldn't have needed this test code to figure this out

Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 9:49 pm
by skywalk
Haha, so you confirm my dismay at this test code.

Re: Do Constants help speed up your Program ?
Posted: Wed Sep 25, 2019 10:20 pm
by BarryG
VB6_to_PBx wrote:Do #Constants i create , help speed up my .EXE Program ?
Not at all. A constant is replaced by its literal value when your exe is compiled.
So this code:
is compiled as this:
Therefore there is no speed difference at runtime.
Re: Do Constants help speed up your Program ?
Posted: Fri Sep 27, 2019 8:36 am
by juergenkulow
Hello skywalk,
I share your dismay, this code for performance analysis does not look pretty at first glance, but the code shows the contents of the CPU time register in CPU clocks. If you have a more effective tool written in Purebasic, I would be grateful for the source code. (eg against measuring outliers)
DisableDebugger, EnableDebugger has to go into the procedures and not into the macro, sorry.
In addition, it makes sense to display CPU clocks in one millisecond.
Greeting Jürgen Kulow