Why stays Global variable empty ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Why stays Global variable empty ?

Post by Vera »

Hello,
again I crossed an issue With Procedure + ProcedureReturn that I can't explain myself.

I was testing around With ElapsedMilliseconds and then placed it inside of a Procedure when I stumbled across the effect that the TEnd-result is not available after the Procedure had been processed although it is a Global variable.

I found out that it makes a difference if I
A - just call the Procedure itself .... [TEnd will have a result]
B - or calling the Procedure for to 'fill' the variable .... [TEnd stays empty*]

Why does this make a difference? And what is the benefit from it?

Code: Select all

Global TStart
Global TEnd

Procedure relTime()
  #Size         = 100000
  Define Space$ = Space(100)

  TStart = ElapsedMilliseconds()
  Debug TStart
  For n = 1 To #Size
    Len = Len(Space$)
    For i = 1 To Len
    Next
  Next
  TEnd = ElapsedMilliseconds() - TStart
  Debug TEnd                            ; the variable contains the result

 ; ProcedureReturn TEnd
EndProcedure


TEnd = relTime()
;relTime()                    ; this call would deliver the result
Debug "TEnd: " + Str(TEnd)   ; why does this var stay empty although it's global?
Debug TStart
Debug TEnd
* [TEnd does not stay empty] if I use ProcedureReturn AND in this case the variable doesn't even have to be made Global beforehand for to deliver the result after the process. Cross-test-example:

Code: Select all

Global TStart
;Global TEnd

Procedure relTime()
  #Size         = 100000
  Define Space$ = Space(100)

  TStart = ElapsedMilliseconds()
  Debug TStart
  For n = 1 To #Size
    Len = Len(Space$)
    For i = 1 To Len
    Next
  Next
  TEnd = ElapsedMilliseconds() - TStart
  Debug TEnd

  ProcedureReturn TEnd
EndProcedure


TEnd = relTime()
;relTime()
Debug "TEnd: " + Str(TEnd)
Debug TStart
Debug TEnd
thanks ~ Vera
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Why stays Global variable empty ?

Post by Tenaja »

All procedures return 0 unless you return a different value. So, unless you have this at the end of the procedure:
ProcedureReturn TEnd
then this TEnd will always be zero:
TEnd = relTime()

Also, if you want to return a value, you should "cast" the procedure (that is, tell PB what the return value Type is):
Procedure.i relTime()
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Why stays Global variable empty ?

Post by Thorium »

On your first example you put the return value of the procedure in TEnd, but the procedure has no return value, which equals 0, so you put the time in and then you put 0 in.

The second example looks fine, of course it does not need to be global. You implicitly declare 2 local TEnd variables. One inside the procedure and one outside. You return the value of the "inside" TEnd and put it in the "outside" TEnd.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Why stays Global variable empty ?

Post by IdeasVacuum »

Use EnableExplicit and name your vars so that you can see what they are

Code: Select all

EnableExplicit

#Size = 100000
Global igTStart.i
Global igTEnd.i

Procedure relTime()
;------------------
;Using Global vars

Protected Space.s = Space(100)
Protected iI.i, iN.i, iLen.i

              igTStart = ElapsedMilliseconds()
              
              For iN = 1 To #Size
              
                   iLen = Len(Space)
              
                   For iI = 1 To iLen
                   Next
              Next
              
              igTEnd = ElapsedMilliseconds() - igTStart
EndProcedure

Procedure.i relTime2()
;---------------------
;Using Local vars

Protected Space.s = Space(100)
Protected iI.i, iN.i, iLen.i, iTStart.i, iTEnd.i

              iTStart = ElapsedMilliseconds()
              
              For iN = 1 To #Size
              
                   iLen = Len(Space)
              
                   For iI = 1 To iLen
                   Next
              Next
              
              iTEnd = ElapsedMilliseconds() - iTStart
              
              ProcedureReturn(iTEnd)
EndProcedure

;Using Global Vars
relTime()
Debug "igTEnd: " + Str(igTEnd)

;Using Local Vars
Define iResult.i = relTime2()
Debug "iResult: " + Str(iResult)

End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Why stays Global variable empty ?

Post by Vera »

Tenaja wrote:All procedures return 0 unless you return a different value.
Thorium wrote:so you put the time in and then you put 0 in.
wow ~ that was the jumping point I missed :D
Thorium wrote:You implicitly declare 2 local TEnd variables.
oh - I wasn't aware about that double declaration.


Also thanks for the other valuable hints and IdeasVacuum summing it all up in one clear example :D

That too finally answers a very old troublesome question, why in some codes I find ProcedureReturn...ing a 'namedVariable' of which I could never find its correspondence in the rest of the code being asked for. (referring to: iTEnd and iResult.i)

Thanks for your quick and helpful responses ~ :)

btw - on a sidenote: playing with this examples I too experienced that the lenght of the variable-names themselves have an impact on the processing time. The shorter the quicker.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Why stays Global variable empty ?

Post by ts-soft »

Vera wrote:btw - on a sidenote: playing with this examples I too experienced that the lenght of the variable-names themselves have an impact on the processing time. The shorter the quicker.
That is bullshit :lol:
There is no different!
(only if you use the debugger for timecount)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Why stays Global variable empty ?

Post by luis »

@Vera

In very simple terms with some imprecisions:

When the program is translated to ASM, the variables are just assigned to sequences of bytes in memory.
The bigger is the data type, the more bytes of memory are required.
For example a .l will require 32 bits (4 bytes) while a .q will require 64 bits (8 bytes).
The machine language instructions generated by the assembler (who processed the ASM file generated by the BASIC compiler) access those bytes through their assigned addresses, the original name used in the PB source is no longer required.
So the variable name length is immaterial, there is no trace of it in the final machine language, only addresses.
What can make a difference in speed is the size of the data accessed, and the addressing method utilized.
There is more than one in machine language and each one is more indicate than another depending on the type of data accessed (a single variable, a string of bytes, an array, etc.) and/or the type of algorithm accessing the data.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Why stays Global variable empty ?

Post by Tenaja »

Vera wrote:btw - on a sidenote: playing with this examples I too experienced that the lenght of the variable-names themselves have an impact on the processing time. The shorter the quicker.
You need to disable the debugger if you want an accurate time measurement.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Why stays Global variable empty ?

Post by Vera »

ts-soft wrote:That is bullshit :lol:
That is insolent and cocksure offensive.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Why stays Global variable empty ?

Post by Vera »

Thanks luis,
I understood that :-)
luis wrote:What can make a difference in speed is the size of the data accessed, and the addressing method utilized.
Ah - this might also explain, why re-running the code various times with only minimal changes, it also returned differences in the debug-results.
So it might just have been a coincidence that the observed difference when I shortened the names was significant against the other observations.
Tenaja wrote:You need to disable the debugger if you want an accurate time measurement.
It dawns to me I've heard about this before, still it's good to be remembered and I'll give it a go.

However - I might be a Formular 1 fan and at times had wanted to become a test pilot - BUT fighting about who might have the speediest routines is not in my interest.
I'm out of contest anyway - or may I say - my PC is ;-)


thanks for your support ~ Vera
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Why stays Global variable empty ?

Post by Tenaja »

Vera wrote:... - this might also explain, why re-running the code various times with only minimal changes, it also returned differences in the debug-results.
So it might just have been a coincidence that the observed difference when I shortened the names was significant against the other observations.
If you did have the debugger on, back-to-back test results will most likely vary wildly even with no changes. The other random things that slow down the debugger are huge compared to the speed hit of using "inefficient" variables.
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: Why stays Global variable empty ?

Post by Thade »

ts-soft wrote:
Vera wrote:btw - on a sidenote: playing with this examples I too experienced that the lenght of the variable-names themselves have an impact on the processing time. The shorter the quicker.
That is bullshit :lol:
There is no different!
(only if you use the debugger for timecount)
Vera wrote:
ts-soft wrote:That is bullshit :lol:
That is insolent and cocksure offensive.
cocksure = schwanzsicher? Wo hast du das her?
Besides the terms you are using here ... if you make a statement (variable names - the shorter the quicker) it should be correct or it is ... bulls*** :lol:
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Why stays Global variable empty ?

Post by PB »

> the variable name length is immaterial

Perhaps not. When I run the following test, the shorter variable
loops are always finished slightly sooner than the longer names.

Maybe someone can point out if I'm doing something wrong?
Because it seems to match Vera's claim... every time. :shock:

Code: Select all

d=400000000

DisableDebugger

start=GetTickCount_()
For ExtremelyLongVariableNameHere=1 To d : Next
test1=GetTickCount_()-start

start=GetTickCount_()
For ExtremelyLongVariableNameHere=1 To d : Next
test2=GetTickCount_()-start

start=GetTickCount_()
For a=1 To d : Next ; Short name (a).
test3=GetTickCount_()-start

start=GetTickCount_()
For a=1 To d : Next ; Short name (a).
test4=GetTickCount_()-start

EnableDebugger

Debug test1 ; 2090 ms
Debug test2 ; 2106 ms
Debug ""
Debug test3 ; 1763 ms
Debug test4 ; 1747 ms
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Why stays Global variable empty ?

Post by IdeasVacuum »

Compile it to an exe (use Msg Requesters to report result).

Code: Select all

d=400000000

start=GetTickCount_()
For ExtremelyLongVariableNameHere=1 To d : Next
test1=GetTickCount_()-start

start=GetTickCount_()
For ExtremelyLongVariableNameHere=1 To d : Next
test2=GetTickCount_()-start

start=GetTickCount_()
For a=1 To d : Next ; Short name (a).
test3=GetTickCount_()-start

start=GetTickCount_()
For a=1 To d : Next ; Short name (a).
test4=GetTickCount_()-start

MessageRequester("Test1", Str(test1),0) ;1328ms
MessageRequester("Test2", Str(test2),0) ;1328ms
MessageRequester("Test3", Str(test3),0) ;1125ms
MessageRequester("Test4", Str(test4),0) ;1109ms
Strange? Possibly to do with the overall load on the CPU at the time, but I expected the results to be near identical.
Last edited by IdeasVacuum on Sun Dec 07, 2014 3:26 pm, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Why stays Global variable empty ?

Post by ts-soft »

Debugger wrote:952
936

951
936
And for sure, you have not use the debugger and not disable it :)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Locked