Shared or reallocating : Wich is faster?

New to FreeBASIC? Post your questions here.
Landeel
Posts: 734
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Shared or reallocating : Wich is faster?

Postby Landeel » Nov 20, 2007 22:42

So my program calls a sub a few hundred times per second to perform some calculations.
I don't know if it's better to dim shared my variables in the main module, or to dim it inside the sub every time it is called. Which is faster?
jofers
Posts: 1525
Joined: May 27, 2005 17:18
Contact:

Postby jofers » Nov 20, 2007 23:11

They're both the same. The only difference when you declare something in a subroutine is that a stack pointer is moved when you call the sub.

If your data is only used by the subroutine, keep it local. Global variables get messy if you have a lot of data.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Nov 20, 2007 23:33

The local variables will be slower, but the difference will typically be no more than a few clock cycles per variable, and unless many variables are involved the call overhead will probably be many times larger. In situations where a few clock cycles actually matter you should probably do the calculations inline, and avoid the overhead from the local variables and the call.

Code: Select all

'====================================================================
#include "windows.bi"
#include "counter.bas"
'====================================================================
'' Counter.bas is available here:
''
'' http://www.freebasic.net/forum/viewtopic.php?t=4221
'====================================================================

dim shared as integer i1,i2,i3,i4

sub test0
end sub

sub test1
  i1 = 1
  i2 = 2
  i3 = 3
  i4 = 4
end sub

sub test2
  dim as integer i1,i2,i3,i4
  i1 = 1
  i2 = 2
  i3 = 3
  i4 = 4
end sub

sleep 3000

counter_begin( 1000, HIGH_PRIORITY_CLASS )
  test0
counter_end
print counter_cycles
counter_begin( 1000, HIGH_PRIORITY_CLASS )
  test1
counter_end
print counter_cycles
counter_begin( 1000, HIGH_PRIORITY_CLASS )
  test2
counter_end
print counter_cycles

sleep

Typical results on a P3:

Code: Select all

103
111
117
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Nov 20, 2007 23:40

Use static variables inside a procedure if the data is large and called a lot.

Stack = Supa Fast, so you really don't have to worry about that much. When it IS a problem though, static variables are like global in that they're always in memory, but they prevent errors as their calling scope is only within the procedure.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Nov 21, 2007 0:45

Jofers, when you Dim a variable, I am fairly sure there is extra work to assign it to 0. This is not automatically done, because in other languages, you will get an error for using a variable before it has been assigned a value. So resetting every element in the array every time the Sub is called is definitely slower. ;-)
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Nov 21, 2007 1:23

I am fairly sure there is extra work to assign it to 0.
Well what the slowdowns are is when you do something like:

dim as integer a
a = 10


a is already defaulted to 0, to you don't gain speed, but you prevent losing it by just doing

dim as integer a = 10

or

dim as integer a = any
a = 10
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Nov 21, 2007 3:35

anonymous1337 wrote:dim as integer a = any


You can do that? Is there a way to do that with arrays?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Nov 21, 2007 6:11

anonymous1337 wrote:Stack = Supa Fast, so you really don't have to worry about that much.

Access to stack tends to be faster than access to the data section because of caching effects. Data caching is better because the stack is frequently accessed, and code caching is better because the instructions that access the stack tend to be smaller than instructions that access the data section. For typical code, running under Windows and on a recent processor, I suspect the effect is very small. My attempts to measure it have not shown any significant difference.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Nov 21, 2007 8:32

As far as I know, there's no way to do '= any' with arrays, but that might be a nice feature request. It would have to be a bit of a quirk though, since the descriptor itself can NOT be uninitialized memory, only the data which it contains.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Nov 21, 2007 8:33

That would be rock hard with large arrays though...And you KNOW it. Sometimes I might just want to make an array bigger for extra storage capacity in say, a dynamic element.

What's the point of doing that (other than the obvious speed gain from not constantly allocating more memory) if it initializes everything to start? D:
RockTheSchock
Posts: 228
Joined: Mar 12, 2006 16:25

Initialising Array with Any

Postby RockTheSchock » Nov 23, 2007 15:21

Try this to initialise array to any.

Dim a(9) As Integer => Any

It seems to work. This little testcode shows that it works with dynamic arrays to and even with Redim Preserve

Code: Select all

Dim i As Integer
Dim As Integer Ptr pa,pb

ReDim b(9) As Integer
For i = LBound(b) To UBound(b)
   b(i)=i+10
   ? b(i),@b(i)
next


pb=@b(0)

? "pb:", *pb
Erase b
? "Erase b"
? "pb:", *pb
?

ReDim a(2) As Integer => Any
'ReDim c(3) As Integer

? "a:"
? a(0),@a(0)
? a(1),@a(1)
? a(2),@a(2)
? a(3),@a(3)
a(30)=123


Sleep
ReDim Preserve a(30) As Integer


? "a:"
? a(0),@a(0)
? a(1),@a(1)
? a(2),@a(2)
? a(3),@a(3)
? a(30),@a(30)




EDIT:

I tested it a bit further and it doesn't really work with Redim and it's not faster. Static arrays are ok. So It's useful with procs.

Here some test's which verify the speed of unitialised static arrays

execution time:

Redim
6.739370020675224

Redim Preserve a(500)
0.8508391846251016

dim without initialise
0.0006.......

dim a(500) instead of a(200000)
0.3614595258586775

for = 1 to 500 instead of 1 to 200000
3.082383073161779



Code: Select all

Sub Test0         
   ReDim a(200000) As Integer        
End Sub

Sub Test1
   ReDim a(0) As Integer      
   ReDim Preserve a(500) As Integer        
End Sub


Sub Test2   
   Dim a(200000) As Integer=>Any
End Sub   

Sub Test3   
   Dim a(500) As Integer
End Sub   

Sub Test4   
   Dim a(200000) As Integer
End Sub   



Dim As Integer i
Dim t As Double


? "Redim"
t= Timer
For i = 1 To 200000
   Test0            
Next
? Timer -t

? "Redim Preserve a(500)"
t= Timer
For i = 1 To 200000
   Test1               
Next
? Timer -t
?

? "dim without initialise"
t= Timer
For i = 1 To 200000
   test2
Next
? Timer -t
?

? "dim a(500) instead of a(200000)"
t= Timer
For i = 1 To 200000
   test3
Next
? Timer -t
?

? "for = 1 to 500 instead of  1 to 200000"
t= Timer
For i = 1 To 500
   test4
Next
? Timer -t


Sleep
end

Last edited by RockTheSchock on Nov 23, 2007 16:49, edited 4 times in total.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Nov 23, 2007 16:12

Cool ^.^ I didn't even test it. Learn something new everyday...
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Nov 23, 2007 19:41

As far as actual practicality, are these two lines the same?
dim as integer a = any
dim as integer a => any
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Nov 24, 2007 3:38

'=>' is only implemented to eliminate ambiguity when assigning fixed-len strings on dimensioning. Take this example:

Code: Select all

dim as string * 3 = "hi"


The compiler would see this:

Code: Select all

dim as string * (3 = "hi")


Which would obviously cause an error. Any other case, '=' and '=>' are completely synonymous.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 10 guests