Page 1 of 5

simple speed optimization request

Posted: Wed Jan 11, 2012 4:19 pm
by Tenaja
Fred/Freak,
For execution speed sake, please make this feature (described in Help) optional:
"If you don't assign an initial value to the variable, their value will be 0. "

For each variable, that feature requires an individual "Push 0", whereas by making it optional it would require only adding a single number to the stack pointer.

Thanks.

Re: simple speed optimization request

Posted: Wed Jan 11, 2012 4:38 pm
by Fred
The problem to this "speed" improvement is all your variables and structures will be left uninitialized, and believe me, it will leads to very hard to find bugs. IHMO the extra push are not a perf killer.

Re: simple speed optimization request

Posted: Wed Jan 11, 2012 5:33 pm
by Tenaja
But with a user-selectable option, anybody enabling it will be aware of it. I, personally, NEVER count on the compiler to initialize anything, just out of good programming practice.

For those of us who use many small procedures, these push/pop combinations are a significant percentage of code.

Re: simple speed optimization request

Posted: Wed Jan 11, 2012 6:01 pm
by Fred
There is no POP ;). Anyway, in PB all is 0 initialised, and i think it's good to leave the same assumption for eveyone.

Re: simple speed optimization request

Posted: Wed Jan 11, 2012 7:28 pm
by Michael Vogel
Macros can be used to replace "many small procedures"...

Re: simple speed optimization request

Posted: Wed Jan 11, 2012 7:54 pm
by Polo
This is something I love about PB, please leave it the way it is :)

Re: simple speed optimization request

Posted: Wed Jan 11, 2012 8:39 pm
by KJ67
Polo wrote:please leave it the way it is :)
+1 :wink:

Re: simple speed optimization request

Posted: Wed Jan 11, 2012 11:40 pm
by MachineCode
KJ67 wrote:
Polo wrote:please leave it the way it is :)
+1 :wink:
+1

Re: simple speed optimization request

Posted: Thu Jan 12, 2012 2:03 am
by Tenaja
Isn't this why we have an OPTIONS dialog box??? Because we don't all like it identical?

Re: simple speed optimization request

Posted: Thu Jan 12, 2012 2:03 am
by Tenaja
Michael Vogel wrote:Macros can be used to replace "many small procedures"...
Not so easy to debug.

Especially with recursion.

Re: simple speed optimization request

Posted: Thu Jan 12, 2012 7:08 am
by wilbert
You can use Static variables.

Re: simple speed optimization request

Posted: Thu Jan 12, 2012 9:46 am
by Shield
wilbert wrote:You can use Static variables.
That's not a solution.
While it's true that this can cause problems and bugs that are hard to find,
modern compilers are able to issue a warning when a variable is being used
that has not been initialized which (almost) nullifies those issues.

So I'm with Tenaja on this, I'd welcome this feature as a compiler option.

Re: simple speed optimization request

Posted: Thu Jan 12, 2012 10:10 am
by Michael Vogel
Statics also do some initializing code, so let's go global... :mrgreen:

Code: Select all

Global a

Procedure test()
	
	;Static a
	
	Debug a
	a+1
	
EndProcedure

a=99
test()
test()
Debug a

Re: simple speed optimization request

Posted: Thu Jan 12, 2012 1:05 pm
by Trond
Honestly, initializing them manually is a bigger performance hit than initializing them automatically.

The last time I checked, static didn't do any initialization when the procedure is called. The default value for a static variable is set at compile time.

Re: simple speed optimization request

Posted: Thu Jan 12, 2012 4:03 pm
by Tenaja
To both Wilbert and Michael V, apparently you both missed my previous post, as both of your options nullify the benefits and the point of recursion.
Trond wrote:Honestly, initializing them manually is a bigger performance hit than initializing them automatically.
Trond, this may be true if you do a lot of For x=0 to 9 loops. However, in one of my 7500+ lines files, I just counted and I have fewer than 70 "= 0" assignments, and I NEVER trust the compiler to initialize them to zero, so this includes all of my For x=0 loops, as well as every define-time initialization.

In fact, this post made me realize that the problem is worse than I thought! This code:
Protected a = 0, b = 0, c = 1

compiles to this:

Code: Select all

  XOR    eax,eax
  PUSH   eax
  PUSH   eax
  PUSH   eax                                                                                                                                                                                                        
; Protected a = 0, b = 0, c = 1
  MOV    dword [esp],0
  MOV    dword [esp+4],0
  MOV    dword [esp+8],1
So even though the compiler "initializes" to zero (using the push, as I was aware), it behaves as if the compiler itself is unaware of the initialization! In essence, good programming practice of initializing your own variables produces WORSE output!

This is the preferred output:

Code: Select all

add esp, #8      ; or whatever the syntax is...
  MOV    dword [esp],0
  MOV    dword [esp+4],0
  MOV    dword [esp+8],1
Because then, when I do NOT initialize them at definition (the norm, since they are usually set at first use), a whole slew of variables (8 in this case) can be allocated like this:
add esp, #24

I do appreciate that at least the compiler uses a loop to initialize large amounts (for smaller output)...however, that consumes even MORE time per variable.

; ------------------------------------------------------
I understand some people will not appreciate the improvement realized within the request. Nevertheless, it can make a difference in both output size (minor) and speed of execution (major) when numerous procedures are called recursively (or repetitively). Plus, it will avoid the "penalty" for coding with the good programming practice of initializing your own variables. And in reality, I must continue to initialize the vars manually myself... afterall, I need the code to read as if the compiler does not, in case I ever port it to another compiler that does not.

I would appreciate if this option is implemented. I understand, Fred, that you have many requests, and I do appreciate you taking the time to consider this.