Author Topic: ParticleSprite Performance  (Read 3644 times)

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
ParticleSprite Performance
« on: 2012-05-31, 01:01:25 AM »
Okay, so after finally implementing a console in the game where I can directly control the game itself I have an issue.

Alright so here are 2 commands in the console that I direct my direction to.

/wind <integer> //Changes the Force Acting on Particles, Wind is an Integer Variable.
/weather <string> // Changes the current Weather

So set the Weather to 'snow' which basically is a reincarnation of Vincent's Snow Sprite Generator.

That runs fine, as long as Wind, the variable that controls the force acting on the snow, is 0.

Here's why:
When a particle is initialized (Created from the Rain/Snow Generator) it calls GetRandomNumber

minimum value: 0
maximum value:Wind

This is done so that not every particle has the exact dx. The particles (snowflakes in this case) move at different speeds.

This means every snowflake/raindrop calls GetRandomNumber on creation.

Performance-wise, I can keep my CPU Usage at 20 when Wind is 0.
When wind is anything else, it goes to around 37, and the game gets an awful spike lag.

Again, as I've said in other topics, performance is key in this game.

There HAS to be a more efficient way to do this, right?

I need to have each particle get a random number to set as their DX, but calling GetRandomNumber on each gives me terrible lag.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: ParticleSprite Performance
« Reply #1 on: 2012-05-31, 04:57:56 AM »
I used the following sample code to test the performance of GetRandomNumber. Does this accurately represent how you are using getRandomNumber?

Code: [Select]
static void Main(string[] args)
{
   DateTime startTime = DateTime.Now;
   double dx = 0;
   int i = 0;
   while (DateTime.Now.Subtract(startTime).TotalSeconds < 1)
   {
      dx += (GetRandomNumber(0, 101) / 100.0f) -0.5f;
      i++;
   }
   Console.WriteLine("{0} {1}", i, dx);
}

static Random randomGen = new Random();
static int GetRandomNumber(int min, int max)
{
   return randomGen.Next(min, max);
}

This performs pretty well. It can generate more than 800,000 random numbers per second. That means it could generate more than 13000 random numbers per frame at 60 FPS, and you shouldn't be needing to generate a random number for every particle on every frame (only the newly added particles), so that should be way more than enough. I suspect something else is going on.

Edit: By the way, using a hard-coded constant in place of GetRandomNumber yields about the same performance, so GetRandomNumber is really not the bottleneck in this test code. When I run the GetRandomNumber function 5 times in each loop, I still get more than 700,000 iterations per second, meaning you could be calling GetRandomNumber more than 3 million times per second or more than 50,000 times per frame.
« Last Edit: 2012-05-31, 05:08:22 AM by bluemonkmn »

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: ParticleSprite Performance
« Reply #2 on: 2012-05-31, 07:32:27 AM »
Hey!  Out of topic, but I'm glad my particleGenerator found a new home! :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: ParticleSprite Performance
« Reply #3 on: 2012-05-31, 03:15:13 PM »
Yikes, In that case I guess I need to analyze more about it..

http://www.youtube.com/watch?v=O8CANO7MkMk

If you have 7 minutes, and your computer doesn't lag too badly from this, I made a video showing exactly what's going on...

I was wrong, each particle calls GetRandomNumber 5 times each, but bluemonkmn, that shouldn't be an issue because of what you're telling me..

I'm not calling any other plans like TestCollisionMask or TestCollisionRect, I'm only calling "If BlockedDown Deactivate"

Any ideas of how to make this faster? In Statistics, we alternatively use a premade data table which has random numbers, but with you're telling me again BluemonkMn, I don't think its the random number that's the issue..

Does SpriteBase lag when too many instances are on screen, even if the rules called by each are minimal?

Hey!  Out of topic, but I'm glad my particleGenerator found a new home! :)

I'll definitely credit you. :)
« Last Edit: 2012-05-31, 05:57:08 PM by #Sharp »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: ParticleSprite Performance
« Reply #4 on: 2012-05-31, 05:59:40 PM »
I can't make out exactly what's going on in the video because it looks like the whole game is constantly severely lagged. And I don't see any difference between when the wind is on and when it's not. Don't you need to figure out what's causing the constant severe lag outside the particles first? It looks like it's running at about a constant 1 FPS, which is simply unplayable. I wouldn't worry about what effect wind and particles are having until you figure out what's slowing the rest down so much.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: ParticleSprite Performance
« Reply #5 on: 2012-05-31, 06:05:00 PM »
Oh no no, I apologize for that, the video recorder I use lags, (I downloaded it today to combat the framerate lag of the popular, but trialware Fraps) but it appears that the framerate is still bogged.

I guess a video wouldn't be appropriate for this purpose then... It's hard to find a recorder that keeps it at the same FPS, with the clarity of HD.

I'll wait until I can release a version for testing, so anyone who decides to help can directly experience the issues..

(Another issue why I can't directly get the FPS is that running it in DebugMode throws an entire boatload of exceptions, because the UserSprite requires 'invalid' operations to function, although none of these invalid operations seem to halt the game outside of DebugMode)

Thanks for at least trying to decipher it, the SWF screen recording was blurry (I couldn't even read the text well)

I'll try to produce a version for testing soon...

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: ParticleSprite Performance
« Reply #6 on: 2012-05-31, 07:12:42 PM »
Exceptions are very expensive. That could be what's slowing things down.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: ParticleSprite Performance
« Reply #7 on: 2012-06-01, 05:40:40 AM »
Thanks for crediting me, I really appreciate. :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com