Do Constants help speed up your Program ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Do Constants help speed up your Program ?

Post 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 ???
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Do Constants help speed up your Program ?

Post by skywalk »

What are you asking :?
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
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Do Constants help speed up your Program ?

Post 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 ?
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Do Constants help speed up your Program ?

Post 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?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Do Constants help speed up your Program ?

Post 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? :D
Forget about possible performance implications that simply don't matter at all and are in no way detectable.
Image
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
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Do Constants help speed up your Program ?

Post 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 .
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Do Constants help speed up your Program ?

Post 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.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Do Constants help speed up your Program ?

Post 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 !
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
juergenkulow
Enthusiast
Enthusiast
Posts: 557
Joined: Wed Sep 25, 2019 10:18 am

Re: Do Constants help speed up your Program ?

Post 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
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Do Constants help speed up your Program ?

Post 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
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Do Constants help speed up your Program ?

Post by skywalk »

What the heck are you comparing exactly?
Explain it like I am 5 years old.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Do Constants help speed up your Program ?

Post 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 :mrgreen:
sorry for my bad english
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Do Constants help speed up your Program ?

Post by skywalk »

Haha, so you confirm my dismay at this test code. :idea:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Do Constants help speed up your Program ?

Post 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:

Code: Select all

#One=1 : AnotherOne=#One
is compiled as this:

Code: Select all

#One=1 : AnotherOne=1
Therefore there is no speed difference at runtime.
juergenkulow
Enthusiast
Enthusiast
Posts: 557
Joined: Wed Sep 25, 2019 10:18 am

Re: Do Constants help speed up your Program ?

Post 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
Post Reply