Do Constants help speed up your Program ?
- VB6_to_PBx
- Enthusiast
- Posts: 625
- Joined: Mon May 09, 2011 9:36 am
Do Constants help speed up your Program ?
Do #Constants i create , help speed up my .EXE Program ?
any differences in .EXE speed , with or without using #Constants i create ???
any differences in .EXE speed , with or without using #Constants i create ???
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: Do Constants help speed up your Program ?
What are you asking
Use #MyConst = 42 instead of MyVar = 42?
This seems quite trivial.
Do you mean #MyGadgetNbr vs #PB_Any?
Use #MyConst = 42 instead of MyVar = 42?
This seems quite trivial.
Do you mean #MyGadgetNbr vs #PB_Any?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
- VB6_to_PBx
- Enthusiast
- Posts: 625
- Joined: Mon May 09, 2011 9:36 am
Re: Do Constants help speed up your Program ?
i mean using #MyConst = 42 -vs- only the number 42 , as the Gadget's numberskywalk wrote:What are you asking
Use #MyConst = 42 instead of MyVar = 42?
This seems quite trivial.
Do you mean #MyGadgetNbr vs #PB_Any?
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 ?
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: Do Constants help speed up your Program ?
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?
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?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Do Constants help speed up your Program ?
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.
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.
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
- VB6_to_PBx
- Enthusiast
- Posts: 625
- Joined: Mon May 09, 2011 9:36 am
Re: Do Constants help speed up your Program ?
i happened to change/replace some Gadget #Constants to just Numbersskywalk 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?
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 .
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: Do Constants help speed up your Program ?
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.
- VB6_to_PBx
- Enthusiast
- Posts: 625
- Joined: Mon May 09, 2011 9:36 am
Re: Do Constants help speed up your Program ?
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 !
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
-
- Enthusiast
- Posts: 557
- Joined: Wed Sep 25, 2019 10:18 am
Re: Do Constants help speed up your Program ?
Hello VB6_to_PBx,
please find the following codes:
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
- VB6_to_PBx
- Enthusiast
- Posts: 625
- Joined: Mon May 09, 2011 9:36 am
Re: Do Constants help speed up your Program ?
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
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
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: Do Constants help speed up your Program ?
What the heck are you comparing exactly?
Explain it like I am 5 years old.
Explain it like I am 5 years old.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Do Constants help speed up your Program ?
It's simple. It is found thatskywalk wrote:What the heck are you comparing exactly?
Explain it like I am 5 years old.
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
sorry for my bad english
Re: Do Constants help speed up your Program ?
Haha, so you confirm my dismay at this test code.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Do Constants help speed up your Program ?
Not at all. A constant is replaced by its literal value when your exe is compiled.VB6_to_PBx wrote:Do #Constants i create , help speed up my .EXE Program ?
So this code:
Code: Select all
#One=1 : AnotherOne=#One
Code: Select all
#One=1 : AnotherOne=1
-
- Enthusiast
- Posts: 557
- Joined: Wed Sep 25, 2019 10:18 am
Re: Do Constants help speed up your Program ?
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
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