Am I not efficient?

the part I closed – [[ ]] slows down my sprites. I have a tween to animate a Sprite for point A to Point B. It Also enlarges the Sprite from point A to point B. It runs pretty smoothly until I add the text below. I’m not sure what I’m doing that would cause it to be unsmooth transition and look like low fps.


clip(WIDTH/15, 0, WIDTH-WIDTH/15, TabPos) 
    
    pushMatrix()
    pushStyle()
    translate(self.originx, self.originy)
    sprite("Documents:Notebook_BG1", 0, 0+dy,self.w,self.h)
    
 --[[   fill(0, 0, 0, 255)
    fontSize(self.fsize*2.5)
    text("Placeholder 1", 0+self.w/2+self.w/32, 0+self.h/1.1+dy)
    fontSize(self.fsize*1.5)
    local _, boxh = textSize("Placeholder")
    text("Placeholder 2", 0+self.w/2+self.w/32, 0+self.h/1.2+dy)
    text("Placeholder 3", 0+self.w/2+self.w/32, 0+(self.h/1.2)-boxh+dy)
    text("Placeholder 4", 0+self.w/2+self.w/32, 0+(self.h/1.2)-boxh*2+dy)
    if self.sel then
        fill(0, 0, 0, 255)
        fontSize(self.fsize*2.5)
        text("Placeholder 1", 0+self.w/2+self.w/32, 0+self.h/1.1+dy)
    end]]
    popMatrix()
    popStyle()
    clip()

@FearMe2142 - I can’t see an obvious reason for a big slowdown, so I would try some different things.

Most obviously, you seem to be separately printing three lines of text above each other. Why not just print one text string, using
to create new lines?

There are a couple of things you can do to get minor improvements.

You use 0+self.w/2+self.w/32 and 0+self.h/1.1+dy and 0+self.h/1.2+dy a lot. Why not pre-calculate them and put them in local variables, it won’t make much difference to speed but your code will be much cleaner.

You also print “Placeholder” 1 twice in the same place on the screen.

@Ignatz alright, thanks for the minor improvements.

If it helps at all the code above is drawn in a class;

function notebook:draw()

code

End

And I have a lot of notebooks.

There is a significant drop though when the text is added :frowning:

Ah, you should have said you had a lot of notebooks.

How many?

@Ignatz

I would like users to add as many as they like, however it’s causing slowdowns

1 seems to work fine.

At 7 I notice significant slowdown.

Drawing hundreds of text lines may definitely cause a slow down. Did you try combining three lines in one, as I suggested?

@Ignatz you my friend, work miracles. Thank you.

@Ignatz

I’ve tried reducing notebooks from 20 per screen to 8 and there is still lag though. Here is my new code inside the Notebook class with around 8 Notebooks.

On the animation is still becomes unsmooth.


if self.originy + dy + self.h > 0 and self.originy + dy < TabPos and self.originx + self.w > WIDTH/15 and self.originx < WIDTH then
    clip(WIDTH/15, 0, WIDTH-WIDTH/15, TabPos)    
    pushMatrix()
    pushStyle()
    translate(self.originx, self.originy)
    sprite("Documents:Notebook_BG1", 0, 0+dy,self.w,self.h)
    fill(0, 0, 0, 255)
    fontSize(self.fsize*2)
    local _, boxh = textSize("Placeholder")
    local x = self.w/2+self.w/32
    text("David Hall \
".."Investments \
".."Dr. Hart", 0+x, 0+self.h/1.3+dy)
    if self.sel then
        
    end
    popStyle()
    popMatrix()
    clip()
    end

It’s difficult to know without doing more tests.

What I would do in a situation like this is isolate the problem, by taking things out (or modifying things) one at a time to see exactly what is causing it. I try to get my code simplified down to the least code that will cause the problem.

You have the clue that it seems related to the number of notebooks, so it is likely to be something you are doing when drawing each one.

For example,

  • comment out the clip commands. Does that make a difference?
  • Comment out the text command
  • Set the fill and font size outside the class, i.e. Instead of setting it over and over again for each notebook, set it once and use that for all the notebooks

If nothing helps, the easiest way for us to assist is if you can provide the simplest code example that will allow us to replicate the problem. Only then can we really see what is going on. Because in many cases, the problem isn’t where you think it is, so it may not be in the above code at all.

Also, when you say 20 notebooks per screen, do you mean you are drawing 20 notebooks each time Codea draws, or something else?

@Ignatz I had in the function draw() I, v in pairs(Display) v:draw() end

And we’re inserting notebooks into Display{}

Table.insert(Display, notebook(x,y,w,h)

ok, I can’t see an obvious problem, but I think you need to do what I suggested, and gradually comment parts out until you find the problem.

You suggested originally that it was the text function, but I think you should confirm it’s that and not something else like clip.

Try loading the sprite image in setup.

notebookImg = readImage("Documents:notebook")

And then sprite(notebookImg). I’ve got a feeling that sprite is smart enough to cache repeated calls, so it might not make a difference, but it’s worth trying.

+1 yojimbo2000

I should have noticed that, you should definitely do that

@yojimbo2000

That made a significant improvement, thank you.

Is this worth doing for anything else? Such as text.

If the text is not going to change, yes, it could be worth making an image containing the text, and sprite that instead of using text.

@Ignatz
@yojimbo2000
Okay, I’ll work on that. I’ve found something interesting.

Restarting codea seems to help. The first time I run the project it’s quite smooth. But after several runs it starts to slow down, however if I restart codea it will go back to performing fine.