Android Question Set function for object

coldteam

Active Member
Licensed User
Longtime User
Hello everyone, there was a question and I hope there is a good solution for it. Please help.

I need to call a function in a render loop for objects. I will give an example to make it clearer.

I am creating a Box object: with properties, id as int, name as string, ....
but the boxes are very different and need to be processed by different functions in a loop.
For example, through CallSub: callsub2(me,"DrawBox_" & box.id,box) (function search many times per second)

Although I could have given an exact reference to the function when creating the object.

I could do the enumeration,
via if or select case, but there can be many types of boxes. And it's not convenient.

I know that in java you can put a link to a function in a variable and call it.
What solution can you recommend? THX :/
 
I know that in java you can put a link to a function in a variable and call it.
This is similar to using CallSub, no?

You can also create different classes with the same "template" and call it with CallSub like this:
Code: Copy
For Each Obj As Object In MyObjects
 CallSub2(Obj, "Draw", Parameter)
Next
 
Upvote 0
This is similar to using CallSub, no?

You can also create different classes with the same "template" and call it with CallSub like this:
Code: Copy
For Each Obj As Object In MyObjects
CallSub2(Obj, "Draw", Parameter)
Next

apparently I have not explained clearly enough

I am trying to avoid using Callsub in a paint loop to avoid looking for a function 60 times per second.

Example
I have 3 sub:

Code: Copy
Sub drawBox_small (box as type_box)

Sub drawBox_medium (box as type_box)

Sub drawBox_big (box as type_box)


I also have objects

Code: Copy
type type_box(name as string)

Dim box1 as type_box
box1.name = small

Dim box2 as type_box
box2.name = medium

Dim box3 as type_box
box3.name = big


now I process in a loop:

Code: Copy
For each box as type_box in BoxList
callsub2(me,"drawBox_" & box.name,box)
next


How can I not use callsub here to do a similar action. No enumeration via else if or select case.
This is the simplest example that I could think of to describe my question.
 
Last edited:
Upvote 0
Please use [code]code here...[/code] tags when posting code.

Have you tested it in release mode and found that there is a performance problem?

60 times per second is a very very slow rate.

Yes, I understand that this is not 1000 ticks per second. But the functions there are heavier than count + 1.
And there are many similar enumerations of groups of objects.
callsub is slower than direct link. So I wanted to clarify if there is a faster option.

I wrote about java, something like that.
Code: Copy
someVar = <getName() method>;
String value = someVar.call();

 private String getName() {
     return "hello";
 }
 
Upvote 0
Have you tested it in release mode and found that there is a performance problem?

This is not a problem now, but I want to greatly expand the functionality and number of objects.
Therefore, I wanted to immediately choose the optimal processing option.
 
Upvote 0
This is called preoptimization. You should start with the simplest solution and if needed optimize it.
The number of methods will not affect the performance. Test it with a few methods. If it is fast enough then forget from this.

ok, I'll try some additional tests.
Such code as I wrote in java is not implemented now in b4a?
Thanks!
 
Upvote 0
There is no such thing in Java. You can call methods by their names but it is more complicated and eventually it will be very similar to using CallSub (if you do it correctly and efficiently).

thanks for answers!
 
Upvote 0
And just to spell it out: Premature optimization is considered a bad thing. There are lots and lots of pages on the interwebs about this, but here's a short section on Wikipedia on the topic.

I wouldn't call it that, I clarified about the callsub analog.
But without looking for a function every time. When there will already be a huge number of functions.
It will be difficult to fix something. A completely different job.
 
Upvote 0
As I see it, the relevant question is:

Q: How does the number of subs affect the performance of CallSub?
A: It doesn't (not talking about millions of subs of course).

and what influences?
how long is the name or something else?
 
Upvote 0
Back
Top