Strange optimization issue

Just starting out? Need help? Post your questions and find answers here.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Strange optimization issue

Post by Derek »

While trying to find a few ways to speed up my program I thought I'd time a few things. In the program below why does it take longer to do the straight forward a=testy etc than the a=testy-1 etc, surely the maths would slow it down.

I was going to put the calculation outside out the loop in my program as it scans a whole picture and could be called millions of times but now I'm not so sure.

Code: Select all

DisableDebugger
OpenConsole()
testy=100
For m=1 To 10
  t=ElapsedMilliseconds()
  For n=1 To 50000000
    a=testy-1
    b=testy-1
    c=testy-1
    d=testy-1
  Next
  t=ElapsedMilliseconds()-t
  PrintN(Str(t))
Next
PrintN("")

For m=1 To 10
  t=ElapsedMilliseconds()
  For n=1 To 50000000
    a=testy
    b=testy
    c=testy
    d=testy
  Next
  t=ElapsedMilliseconds()-t
  PrintN(Str(t))
Next
PrintN("")
Input()
I know an average size photo is probably about 6-8 million pixels but when you add up the four loops used in my program along with a few dozen IF's in each then every millisecond is going to count.
Last edited by Derek on Wed Apr 30, 2008 4:39 pm, edited 1 time in total.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

WTF :? :shock:
I like logic, hence I dislike humans but love computers.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I'm no ASM expert but maybe it's a PUSH/POP vs MOV issue?

Partial ASM output from the variable=testy-1 loop...

Code: Select all

CALL _PB_ElapsedMilliseconds@0
 MOV dword [v_t],eax
 MOV dword [v_n],1
_For3:
 MOV eax,50000000
 CMP eax,dword [v_n]
 JL _Next4
 MOV ebx,dword [v_testy]
 DEC ebx
 MOV dword [v_a],ebx
 MOV ebx,dword [v_testy]
 DEC ebx
 MOV dword [v_b],ebx
 MOV ebx,dword [v_testy]
 DEC ebx
 MOV dword [v_c],ebx
 MOV ebx,dword [v_testy]
 DEC ebx
 MOV dword [v_d],ebx
_NextContinue4:
 INC dword [v_n]
 JMP _For3
_Next4:
 CALL _PB_ElapsedMilliseconds@0
Partial ASM output from the variable=testy loop...

Code: Select all

CALL _PB_ElapsedMilliseconds@0
 MOV dword [v_t],eax
 MOV dword [v_n],1
_For3:
 MOV eax,50000000
 CMP eax,dword [v_n]
 JL _Next4
 PUSH dword [v_testy]
 POP dword [v_a]
 PUSH dword [v_testy]
 POP dword [v_b]
 PUSH dword [v_testy]
 POP dword [v_c]
 PUSH dword [v_testy]
 POP dword [v_d]
_NextContinue4:
 INC dword [v_n]
 JMP _For3
_Next4:
 CALL _PB_ElapsedMilliseconds@0
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Weird indeed!

Code: Select all

a=testy-0
is twice as fast as

Code: Select all

a=testy
Time for some optimization I guess :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Now that is crazy. Do we do the optimisation by putting -0 all over the place or do you reckon it will be in the compiler.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Who wants to place a bet that it'll be fixed for Beta 5 ;)
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

You know what, I thought I had seen this somewhere before but didn't look that far back. :oops:

Still, you'd of thought it might of been fixed by now. :wink:
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

DoubleDutch wrote:Now that is crazy. Do we do the optimisation by putting -0 all over the place or do you reckon it will be in the compiler.
I sure hope they optimize the compiler, would be nice if the team comment on this.
I like logic, hence I dislike humans but love computers.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Just a note: This happens only outside procedures. Inside procedures even a direct assignment uses mov. So just structure your code and it will be fast. :wink:
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Code: Select all

DisableDebugger 
Procedure test()
OpenConsole() 
testy=100 
For m=1 To 10 
  t=ElapsedMilliseconds() 
  For n=1 To 50000000 
    a=testy-1 
    b=testy-1 
    c=testy-1 
    d=testy-1 
  Next 
  t=ElapsedMilliseconds()-t 
  PrintN(Str(t)) 
Next 
PrintN("") 

For m=1 To 10 
  t=ElapsedMilliseconds() 
  For n=1 To 50000000 
    a=testy 
    b=testy 
    c=testy 
    d=testy 
  Next 
  t=ElapsedMilliseconds()-t 
  PrintN(Str(t)) 
Next 
PrintN("")  
EndProcedure
test()
testy=100 
For m=1 To 10 
  t=ElapsedMilliseconds() 
  For n=1 To 50000000 
    a=testy-1 
    b=testy-1 
    c=testy-1 
    d=testy-1 
  Next 
  t=ElapsedMilliseconds()-t 
  PrintN(Str(t)) 
Next 
PrintN("") 

For m=1 To 10 
  t=ElapsedMilliseconds() 
  For n=1 To 50000000 
    a=testy 
    b=testy 
    c=testy 
    d=testy 
  Next 
  t=ElapsedMilliseconds()-t 
  PrintN(Str(t)) 
Next 
PrintN("") 
Input() 
On mine it is even worse inside procedures so it looks like structured programming is out. :(
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

HOW MANY *^!"#¤%&/()=?=)(/&%¤# TIMES DO I HAVE TO SAY THIS? TURN OFF THE DEBUGGER WHEN SPEED TESTING!
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

LOOK AT THE *^!"#¤%&/()=?=)(/&%¤# FIRST LINE OF CODE. :wink:

But even if I turn off the debugger first, which does in fact make a difference ( :wink: to Trond) then my fastest result is from the -1 code outside of the procedure and my slowest result is from the straight a=testy code outside the procedure, inside the procedure both results are roughly equal.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Local variables will probably be a bit slower since they are accessed relative to the stack instead of directly. However, the fastest one here (with the debugger truly OFF), is the second one inside the procedure. It beats the -1 outside by only a small margin, which is turned around when I write to file instead of to the console, so it's probably a caching/pipelining/alignment coincidence.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

The DisableDebugger part really should work in my opinion. (But the fact is, that it doesn't completely work.) Maybe it's a bug?
Post Reply