Constants...

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Skipsy.

Hi folks,

For the time being I am using variables as constantes.
I am not satisfied by this solution because the processor spends
more clock time when accessing to the content of memory address.

For example, in a test :
if x > 100 ; generated code is fast
if x > maximum ; generated code is slower because of the
memory access reading 'maximum'

Is there any way to use real constant ? I mean I 'd like to be able to use a word that will be replace by his value when compiling.
Equivalent of PB constant (#PB_Key...)

Thks a lot.

"If you want to touch the sky,
f**k a duck and try to fly"
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

#MyConstant = 61873
#MyText.s = "Hello World"

You can define like this what ever you want.

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Skipsy.

Wow quick answer...
I was just preparing a new post saying that I found out the
solution (not documented but so obvious) when you posted your
answer.

Many thks.

"If you want to touch the sky,
f**k a duck and try to fly"
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Saboteur.

Really is fast???

Try this:

Code: Select all

#lim=50
#inc=1
#zer=0
lim=50
inc=1
zer=0
If OpenConsole()
  a.l=0

  tm1=GetTickCount_()
  For f=0 To 500000000
    If a<#lim
      a+#inc
    Else
      a=#zer
    EndIf
  Next f
  tm2=GetTickCount_()-tm1
  PrintN("Constant time: " + Str(tm2))

  tm1=GetTickCount_()
  For f=0 To 500000000
    If a<lim
      a+inc
    Else
      a=zer
    EndIf
  Next f
  tm2=GetTickCount_()-tm1
  PrintN("Variable time: " + Str(tm2))

  Input()
EndIf

End
Result (Athlon 1200, Win98):

Constant time: 3499
Variable time: 3096
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Lollypop.

disagree

Constants 6,4 secs
Variables 9,1 secs

PII-500
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

Constants: 2804
Variables: 3205

P4 1.6GHz XP Pro



Regards,

Berikco

http://users.pandora.be/berikco/purebasic.htm
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Denis.

With debugger :

Constants: 33589
Variables: 33185

Without debugger :

Constants: 2174
Variables: 2450

Win98 SE , Celeron 2GHz


Denis
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

constants: 5168
variable: 4556

AMD Duron 800 win2k

seems that the AMD family benefits from using variables instead of constants...

funny thing is that when compiling with /commented switch i get aproximately the opposite result? (i.e. variable = 5150 and constant = 4500, how strange is that?)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by Pupil

seems that the AMD family benefits from using variables instead of constants...
Different sizes of instruction and data caches between manufacturers / families of processors?

The OS speeds may differ when accessing code and data memory?


--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.62, external editor)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Saboteur.
constants: 5168
variable: 4556
AMD Duron 800 win2k
seems that the AMD family benefits from using variables instead of constants...
funny thing is that when compiling with /commented switch i get aproximately the opposite result? (i.e. variable = 5150 and constant = 4500, how strange is that?)
hmmm... without debugger and with /commented, i get same result.
strange :):)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Processor caches makes worlds crazy...

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Skipsy.

Wow :

With debugger
Constants: 32797
Variables: 32766

Without debugger :

Constants: 2141
Variables: 2421

(P4 2Ghz 256mo)

To be honest I was considering that loading a registry with a
constant was A LOT faster than accessing to a memory address because
of my experience (a long time ago) with Zilog or 68000 proc.
The difference exists but not so obvious !!!



"If you want to touch the sky,
f**k a duck and try to fly"
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Revolver.

Constant time: 13597
Variable time: 17849

P2 333Mhz, 128MB SDRAM
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

No Debugger:
Constant time: 2463
Variable time: 2173

AMD XP2100+ (1.733Ghz)

:)


--Kale

In love with PureBasic! :)
Post Reply