Globals or Locals

Started by Baggey, December 16, 2024, 10:07:51

Previous topic - Next topic

Baggey

@Derron

Is it better to declare as much variables locally rather than Globaly for increased Performance?

I see Pascal does the local stuff a lot?
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

dawlane

#1
Quote from: Baggey on December 16, 2024, 10:07:51Is it better to declare as much variables locally rather than Globaly for increased Performance?

I see Pascal does the local stuff a lot?

The performance of a local depends on what type you are using and how the language implements locals.

With pascal, then you should look a little harder. Locals are used in procedures and functions. Global's are used where the direct access is required to an value or object.
With Object Pascal class types you have to be careful as procedures and functions can be passed by reference without you knowing it. You should generally take it that objects are passed by reference.
Normal types are by value, unless the procedure/function uses the var keywords before the parameter, than that parameter is passed by reference.

Derron

if you declare a global in "global scope" and "locals in global scope" there should not be differences.

Globals inside of Types are a bit special as you for now have no guaranteed order of when they are initialized ... so relying on globals in foreign types is not guaranteed to work (eg when "global g:mytype = new mytype" reads from a global "TOtherType.myglobal").


bye
Ron

Baggey

Thanks for the reply Derron.

I use many Types. So if my types are accessing external globals should i passing them in? for better speed in execution that is?

or have i misunderstood?
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

Derron

SuperStrict
Import Brl.StandardIO

Type TColor
  Field r:Int, g:Int, b:Int

  Method New(r:Int, g:Int, b:Int)
    Self.r = r
    Self.g = g
    Self.b = b
  End Method
End Type


Type TCar
  Field color:TColor
  Global defaultCar:TCar = New TCar(TCarManufacturer.color1)

  Method New(c:TColor)
    Self.color = c
    Print "New car with color " + c.r + ", " + c.g +", " + c.b
  End Method
End Type


Type TCarManufacturer
  Global color1:TColor = New TColor(255,0,0)
End Type


'reenable and it somehow works...
'Local car:TCar = New TCar(TCarManufacturer.color1)

Depending on WHERE things are defined, this can lead to issues. Here the "TCar" type is created before TCarManufacturer ... it simply goes "top to down". Result is that "color1" is not defined yet and thus "null" (eav happens). But if you check the code you will see, that it has a dependency to a "more at the bottom" global ... so you normally would say: "resolve what you can and then start again at the top, resolve what is possible now ... do until everything is resolved or one iteration without resolve happened".


bye
Ron

Baggey

Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!