Hi Mathieu,
in the mentioned widget that I've created, I'm creating and drawing 93 labels, and these are the profiling statistics:
- Using kivy.uix.label.Label: 29731 function calls in 0.160 seconds
- Using kivy.core.text.Label: 16171 function calls in 0.066 seconds
The function calls are not exclusive for calls to the uix.Label or CoreLabel, but for my widget's draw method. But the only difference between both statistics is that in one I add uix.label to the canvas and in another, I use a text.label texture to add a rectangle to my widget's canvas.
As you said, the second approach has the problem for when moving/resizing the widget. But for my quick prototyping, I did prefer to have faster initial drawings than (faster) subsequent redrawings. Also, I don't know if relying on kv lang to redraw my labels would be much faster, since any change in a uix.label property triggers the redraw method of CoreLabel, and that is the only thing that I do.
Probably in the near future I'll rely on kivy lang, but it would be great if one could speed up the __init__ of uix.labels.
As should be obvious now, since uix.label eventually also invokes CoreLabel, it is faster to call CoreLabel. But here is a working example:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
import cProfile
class TestApp(App):
def build(self):
widget = Widget()
cProfile.runctx('self.labels(100)', globals(), locals())
cProfile.runctx('self.corelabels(100)', globals(), locals())
return widget
def labels(self, n):
for i in xrange(n):
Label(text="%s" % i)
def corelabels(self, n):
for i in xrange(n):
CoreLabel(text="%s" % i)
if __name__ == "__main__":
TestApp().run()
In my computer, these are the statistics:
- self.labels: 18330 function calls (18329 primitive calls) in 0.073 seconds
- self.corelabels: 503 function calls in 0.002 seconds
The creation of uix.Label is too much expensive for some simple drawing cases, since it does binds to listeners, etc. I guess this is not something new, but the difference in magnitude seems to be large.. This is specially relevant in my low-spec Android tablet.
I Hope this is of some assistance.. Regards,
João Ventura