Is any difference (in speed of my program – execution time) between??
1 st option:
private void particleReInit(int loop)
{
this.particle[loop].active = true;
this.particle[loop].life = 1.0f;
this.particle[loop].fade = 0.3f * (float)(this.random.Next(100)) / 1000.0f + 0.003f;
this.particle[loop].r = colors[this.col][0]; // Select Red Rainbow Color
this.particle[loop].g = colors[this.col][1]; // Select Red Rainbow Color
this.particle[loop].b = colors[this.col][2]; // Select Red Rainbow Color
this.particle[loop].x = 0.0f;
this.particle[loop].y = 0.0f;
this.particle[loop].xi = 10.0f * (this.random.Next(120) - 60.0f);
this.particle[loop].yi = (-50.0f) * (this.random.Next(60)) - (30.0f);
this.particle[loop].xg = 0.0f;
this.particle[loop].yg = 0.8f;
this.particle[loop].size = 0.2f;
this.particle[loop].center = new PointF(particleTextures[0].Width / 2, particleTextures[0].Height / 2);
}
2 nd option:
Particle p = particle[loop];
p.active = true;
p.life = 1.0f;
...
Where Particle particle[] = new Particle[NumberOfParticles]; is just an array of Particles which have some properties like life, position.
I’m doing it in Visual Studio 2010 Like WFA (Windows Form Aplication) and need to enhance performance (we’re not able to use OpenGL, so for more particles my program tends to be slow).
I would certainly expect there to be a difference in speed – it’s doing more work, after all. Another thread may have changed the contents of the array between statements, which may (or may not) be visible within this thread. If these are properties rather than fields, the property setter could even conceivably change the values in the array within the same thread, which would have to be visible.
Whether the difference in speed is significant or not is a different matter, and one that we can’t judge.
More importantly, I’d say that the second form is clearer than the existing code.
In fact, if this is actually meant to be reinitializing the whole element, I’d actually create a new
Particleand then assign that to the element:… or create a separate method/constructor which created a particle in an appropriate state.