Time difference in loop

Blitzer

New Member
Licensed User
Hello,

so what is that?
(1.loop = 945ms / 2.loop = 48 ms)

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Public d,x,y As Int
    Public Feld(1921,1081) As Int
    Public RGB=5646469 As Int
End Sub

Sub AppStart (Args() As String)
    
    Dim n As Long = DateTime.Now
    For d = 0 To 100
        For y = 0 To 1080
            For x = 0 To 1920
                Feld(x,y)=RGB
            Next
        Next
    Next
    Log($"1.Lauf: ${DateTime.Now - n}ms"$)
        
    Dim n As Long = DateTime.Now
    For d = 0 To 100
        For x = 0 To 1920
            For y = 0 To 1080
                Feld(x,y)=RGB
            Next
        Next
    Next
    Log($"2.Lauf: ${DateTime.Now - n}ms"$)
        
End Sub
Blitzer
 

Blitzer

New Member
Licensed User
Hello,

it's a code snippet, but the result is not logical.
The array is the size of a screen...
1. loop = 1080 and 1920
2. loop = 1920 and 1080
The 2. loop is factor 20 faster!!!

Blitzer
 

emexes

Well-Known Member
Licensed User
My first thought is that you are aggravating the Cache Gods, especially if the array is not aligned to a 4-byte multiple, or the cache lines are large (eg 16 bytes).

The first case is accessing memory in giant steps spaced (1081 x 4) bytes apart, and probably every access causes a cache line miss/load. The second case is accessing memory sequentially, and one cache line miss/load serves multiple memory accesses.

btw what's with the extra-pixel vertically and horizontally in excess of HD 1920 x 1080?
 

sorex

Expert
Licensed User
isn't the reason that the feld array object has been used already (skipping init or pointer creations) ?

I notice this in some of my heavy routines aswell. second runs of the same code are always (a lot) faster.
 

emexes

Well-Known Member
Licensed User
I notice this in some of my heavy routines as well. second runs of the same code are always (a lot) faster.
1/ <JOKE>Never say never... or always...</JOKE>
2/ the speedups you observe are probably due to the just-in-time / runtime compilation needing to be done for the first run, with the compiled code still available (cached) for the second run ie no need to compile it again
 

OliverA

Expert
Licensed User
See this for an explanation how java handles a 2d array: https://stackoverflow.com/a/6631081

Now look at loop one. You are changing x for each iteration. That means you are loading a new pointer for each sub-array just to set one value of that sub-array. So you are switching sub-array pointers 1081x1921 times just to set 1081x1921 items.

In the second loop, you are switching sub-array pointers only 1921 times in order to set 1081x1921 items. So yes, you just sped up your 2d array access by quite a bit and that is reflected properly by the shortened runtime of your second example

Update: additionally, you are gaining benefits of the sequential access of the sub-arrays as pointed out by @emexes and @Erel
 
Top