simple speed optimization request

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

simple speed optimization request

Post 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.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: simple speed optimization request

Post 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.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: simple speed optimization request

Post 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.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: simple speed optimization request

Post 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.
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: simple speed optimization request

Post by Michael Vogel »

Macros can be used to replace "many small procedures"...
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: simple speed optimization request

Post by Polo »

This is something I love about PB, please leave it the way it is :)
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

Re: simple speed optimization request

Post by KJ67 »

Polo wrote:please leave it the way it is :)
+1 :wink:
The best preparation for tomorrow is doing your best today.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: simple speed optimization request

Post by MachineCode »

KJ67 wrote:
Polo wrote:please leave it the way it is :)
+1 :wink:
+1
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: simple speed optimization request

Post by Tenaja »

Isn't this why we have an OPTIONS dialog box??? Because we don't all like it identical?
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: simple speed optimization request

Post by Tenaja »

Michael Vogel wrote:Macros can be used to replace "many small procedures"...
Not so easy to debug.

Especially with recursion.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: simple speed optimization request

Post by wilbert »

You can use Static variables.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: simple speed optimization request

Post 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.
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
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: simple speed optimization request

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: simple speed optimization request

Post 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.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: simple speed optimization request

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