Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

What slow codea down more, images or shapes?

edited September 2013 in Questions Posts: 577

I've had a question in my head for awhile, what would slow done codea more, a rectangle or a image?

Comments

  • dave1707dave1707 Mod
    Posts: 7,611

    That depends on the size of the image and the rectangle. Is the rectangle filled with a color. There are quite a few variables that aren't being told.

  • Posts: 577

    Oh sorry, lets say the rectangle has a color in it, and lets say the sprite is a resized
    1 x 1, stretched to 20 by 20, and its tinted.

  • dave1707dave1707 Mod
    edited September 2013 Posts: 7,611

    I dont know if this is accurate or not, but this executes a sprite command 10,000 times and then executes a rect command 10,000 times. The sprite image and rectangle are both 101x171. The images won't show, but the time it takes to execute each is printed in seconds. Here are the values I get on my iPad1. This takes about 12 seconds to run.


    image 0.177628 rect 1.50384

    The rect command takes about 9 times longer.


    function setup()     img=readImage("Planet Cute:Character Boy")     w=WIDTH/2     h=HEIGHT/2     run=1 end function draw()     background(40, 40, 50)     if run==1 then         image1()         run=2     end     if run==2 then         rect1()         run=3     end end function rect1()     a=os.clock()     for z=1,10000 do         rect(w,h,101,171)     end     b=os.clock()     print("rect  ",b-a) end function image1()     a=os.clock()     for z=1,10000 do         sprite(img,w,h)     end     b=os.clock()     print("image  ",b-a) end
  • Posts: 577

    Ok, thanks! :)

  • SimeonSimeon Admin Mod
    Posts: 4,958

    @dave1707 it will also depend on whether you are using smooth() / noSmooth().

    I expect that noSmooth() rects are the fastest, but still slower than sprites in your benchmark (you are drawing sprites in such a way that they will be batched into a single draw call, a very optimal scenario).

  • dave1707dave1707 Mod
    Posts: 7,611

    @Simeon Now I'm confused. I re-ran the above program 10 times and averaged the values. I changed the rect and sprite lines to use math.random(WIDTH) and math.random(HEIGHT) instead of using the fixed w and h values. I ran that code 10 times and averaged the values. These are the results using fix values and math.random(). The time increased for both of them, but more for the image. And adding math.random() didn't seem to matter that much over the fixed values.


    fixed image 0.165703 rect 1.22362 math.random image 0.198421 rect 1.22489
Sign In or Register to comment.