Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Why does rendering time vary?  (Read 1743 times)

0 Members and 1 Guest are viewing this topic.

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Why does rendering time vary?
« on: January 23, 2012, 03:25:34 am »
When I run this app, the first few iterations consistently take much less time compared to latter iterations, can anyone explain why?

Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>


using namespace std;


int main()
{
float time = 0;
   

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
   
sf::Image image;
image.LoadFromFile("1024x1024.png");
sf::Sprite sprite;
sprite.SetImage(image);
sprite.SetPosition(0,0);

sf::Clock clock;

for(int i=0; i<10; i++)
{
clock.Reset();
App.Clear();
for(int i=0; i<5000; i++)
{
App.Draw(sprite);
}
App.Display();
time = clock.GetElapsedTime();

printf("time: %f \n", time);
}

int x;
std::cin>>x;

    return EXIT_SUCCESS;
}


This is the output I get:
https://legacy.sfmluploads.org/cache/pics/197_Capture.PNG

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Why does rendering time vary?
« Reply #1 on: January 24, 2012, 05:22:47 am »
So I started doing some more tests for optimization and I just cant figure it out.

This new code does some various tests. Its pretty self explanatory.
Whats interesting to me is that the same operation might take 20 times longer one run to the next. Here is my output: https://legacy.sfmluploads.org/cache/pics/198_Capture.PNG

Take a look at the last test. The one drawing 2000 1x1 pixel sprites.
In my output, the first 3 results are about 90 times slower than the following samples.

I just cant figure out why there would be such a large discrepancy in the numbers. Is there something going on under the hood that would contribute to this? Maybe my logic is just off? Maybe this is just normal? If anyone has any advice to optimize rendering I would love to hear it.

Here are the tests:
Code: [Select]
int main()
{

sf::Image image_1;
image_1.LoadFromFile("1x1.png");
sf::Sprite sprite_1;
sprite_1.SetImage(image_1);

sf::Image image_2048;
image_2048.LoadFromFile("2048x2048.png");
sf::Sprite sprite_2048;
sprite_2048.SetImage(image_2048);

sf::Image image_2048_2;
image_2048_2.LoadFromFile("2048x2048_2.png");
sf::Sprite sprite_2048_2;
sprite_2048_2.SetImage(image_2048_2);


float time = 0;
   
// Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
   

sf::Clock clock;

///////////////////////////////////////////////////////////////////////////////////////////////////////

for(int i=0; i<10; i++)
{
clock.Reset();
App.Clear();
for(int i=0; i<2000; i++)
{
App.Draw(sprite_2048);
}
App.Display();
time = clock.GetElapsedTime();

printf("draw one 2048x2048 2000 times: %f \n", time);
}
printf("\n\n");

///////////////////////////////////////////////////////////////////////////////////////////////////////

for(int i=0; i<10; i++)
{
clock.Reset();
App.Clear();
for(int i=0; i<1000; i++)
{
App.Draw(sprite_2048);
App.Draw(sprite_2048_2);
}
App.Display();
time = clock.GetElapsedTime();

printf("draw two 2048x2048 alternating 1000 each: %f \n", time);
}
printf("\n\n");

///////////////////////////////////////////////////////////////////////////////////////////////////////

for(int i=0; i<10; i++)
{
clock.Reset();
App.Clear();
for(int i=0; i<1000; i++)
{
App.Draw(sprite_2048);
}
for(int i=0; i<1000; i++)
{
App.Draw(sprite_2048_2);
}

App.Display();
time = clock.GetElapsedTime();

printf("draw two 2048x2048 batched 1000 each: %f \n", time);
}
printf("\n\n");

///////////////////////////////////////////////////////////////////////////////////////////////////////

for(int i=0; i<10; i++)
{
clock.Reset();
App.Clear();
for(int i=0; i<2000; i++)
{
App.Draw(sprite_1);
}
App.Display();
time = clock.GetElapsedTime();
printf("draw one 1x1 2000 times: %f \n", time);
}
printf("\n\n");

///////////////////////////////////////////////////////////////////////////////////////////////////////

int x;
std::cin>>x;

    return EXIT_SUCCESS;
}

 

anything