Android Question Weird create label in clv issue ??

ilan

Expert
Licensed User
Longtime User
hi
i just encounter a really weird behavior. i am not sure it is related to the new update or not.

so what i did is adding each letter in a specific text to a clv.
i add only a single view. it is a label.

i made some speed test and i had big differences between 2 methods.

method 1:

B4X:
Private Sub createitem1(i As Int, width As Float, height As Float) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.Color = xui.Color_White
    p.Width = width
    p.Height = height
   
    Dim natLbl As Label
    natLbl.Initialize("")
   
    Dim lbl As B4XView = natLbl
    lbl.Text = "item: " & i
    lbl.TextColor = xui.Color_ARGB(255,Rnd(0,255),Rnd(0,255),Rnd(0,255))
    lbl.SetTextAlignment("CENTER","CENTER")
   
    p.AddView(lbl,0,0,width, height)
    Return p  
End Sub

so i create inside the function a nativelabel and reference it to a b4xview.
this method works fine and fast.

then i tried to add the label from a outside function to be able to use it again.
like this:

B4X:
Private Sub createitem2(i As Int, width As Float, height As Float) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.Color = xui.Color_White
    p.Width = width
    p.Height = height
   
    Dim lbl As B4XView = createLabel("")
    lbl.Text = "item: " & i
    lbl.TextColor = xui.Color_ARGB(255,Rnd(0,255),Rnd(0,255),Rnd(0,255))
    lbl.SetTextAlignment("CENTER","CENTER")
   
    p.AddView(lbl,0,0,width, height)
    Return p
End Sub

Private Sub createLabel(event As String) As B4XView
    Dim natLbl As Label
    natLbl.Initialize(event)
    Return natLbl
End Sub

this method took much longer (x50 longer)

1. method (in debug mode) 80ms
2. method (in debug mode) 4000ms

so first i thought it has something to do with creating the label from outside BUT accidentally i changed the outside method to NOT use the passed variable and it took the same time as the 1 method like this:

B4X:
Private Sub createLabel(event As String) As B4XView
    Dim natLbl As Label
    natLbl.Initialize("")
    Return natLbl
End Sub

as you can see EVENT is not used and it takes now only 80 ms as the first method. why is it like this that if i used the passed object it takes 50x longer to create this clv than not using the passed object??

logs:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
6495 ms
82 ms
70 ms

(example included)
 

Attachments

  • test.zip
    4.9 KB · Views: 63

TILogistic

Expert
Licensed User
Longtime User
And if you use the xui views utilities, does the same thing happen to you?
B4X:
Dim lbl As B4XView = XUIViewsUtils.CreateLabel
What version of B4a do you use?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
And if you use the xui views utilities, does the same thing happen to you?
B4X:
Dim lbl As B4XView = XUIViewsUtils.CreateLabel
What version of B4a do you use?
this works fine, but how do you add the event name if u use this method?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
6495 ms
82 ms
70 ms
I don't know how you got the result, I tested on the emulator and my phone, there was almost no difference
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
75 ms
72 ms
86 ms
 
Upvote 0

teddybear

Well-Known Member
Licensed User
when you run the example try to make change in the code and while still running test again.
this method took much longer (x50 longer)

1. method (in debug mode) 80ms
2. method (in debug mode) 4000ms
You mean that change code in debug mode, if you change code in method createitem1, it also works slow, it should be an injected debugging code doing something for the changes
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
The times vary depending on the button pressed, i.e. the one you think is slower is sometimes faster.

Looking at the CustomListview code, I see that the objects are stored in a variable called Items of type list.

The clear method only performs an items.Clear which also affects the times.

That's what I can see.

1723200341447.png
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is no real difference between the three methods.
This is the output after 1000 tests:

(MyMap) {1=[IsInitialized=true, Name=method 1, average=103.95705521472392
, n=326], 2=[IsInitialized=true, Name=method 2, average=103.44277108433735
, n=332], 3=[IsInitialized=true, Name=method 3, average=104.50292397660819
, n=342]}


B4X:
Type TimingMeasurement (n As Int, average As Double, Name As String)

B4X:
For i = 1 To 3
    tms.Put(i, CreateTimingMeasurement(0, 0, "method " & i)) 'global Map
Next
For i = 1 To 1000
    CallSub(Me, "Button" & Rnd(1, 4) & "_Click")
    If i Mod 10 = 0 Then
        Log(i)
        Log(tms)
        Sleep(0)
    End If
Next

B4X:
Private Sub Button1_Click
    Dim startime As Long = DateTime.Now
    CustomListView1.Clear
    For i = 0 To 500
        CustomListView1.Add(createitem1(i,CustomListView1.AsView.Width,CustomListView1.AsView.Height/14),i)
    Next
    AddMeasurement(startime, 1)   
End Sub

Private Sub AddMeasurement(Start As Long, Method As Int)
    Dim tm As TimingMeasurement = tms.Get(Method)
    tm.average = (tm.average * tm.n + DateTime.Now - Start) / (tm.n + 1)
    tm.n = tm.n + 1
End Sub
 
Upvote 0
Top