Hi, I'm working on a particle app in c / c++ and openGl
First I wrote the whole piece of code on C ( structures and I was allocatig memory with new() ) And taht's the left picture. But I thought it was a bad idea to use structures and all that when you can use the c++ function such as vector().
So i tried it and it was very nice to use the functions and the code is much cleaner but the program is so slow "/ just look at the picture to the right. the numbers of particles is fps based.
[IMG]http://a.imageshack.us/img62/595/effectcvscpp.png[/IMG]
Do you guys know if I'm doing anything wrong?
I can give you some code if you want.
Give us the code.
[code]
struct sParticle
{
sParticle() {}
CVector3 pos;
CVector3 direction;
};
class cEffect
{
private:
vector <sParticle> particle;
public:
// Get particle data
sParticle Particle(int Part);
// Add a new particle
void addParticle( CVector3 Pos, CVector3 Dir);
....... and so on
[/code]
Please don't blame me if I'm doing this wrong :P
That's only the header, we need to see the particle adding and rendering code.
If you're using std::vector wrong, that could definitely be the problem.
Such as adding lots of particles into the vector in a loop. Because you are storing instances and not pointers in there, it'll take lots of time to resize the vector each and every time you add a new instance into it after some point.
But eh, I dunno. Need to see the code first.
If you're certain it's the memory allocation scheme that is causing it, you can solve it with [url=http://www.cplusplus.com/reference/stl/vector/reserve/]vector::reserve[/url].
[QUOTE=esalaka;24689086]That's only the header, we need to see the particle adding and rendering code.[/QUOTE]
oh well
for drawing, in the opengl rendering function
[code]
for(int i = 0; i < Effect.ParticleAmount(); i++)
{
drawLine( Effect.Particle(i).pos, Effect.Particle(i).direction, 10);
}
[/code]
and the other piece of code for adding,moving and removing particles in the main loop
[code]
//add particle
if(Mouse[0])
{
for(int i = 0; i <60; i++)
{
CVector3 newDirection = Rotate2d( CVector3(0,-RandomNumber(250,50),0) , RandomNumber( 1, 359));
Effect.addParticle( mousePos, newDirection);
}
}
//move all particle
for(int i = 0; i < Effect.ParticleAmount(); i++)
{
Effect.setParticlePos( i, Effect.Particle(i).pos + Effect.Particle(i).direction * frameTime);
// add the down vector
Effect.setParticleDir(i, CVector3( Effect.Particle(i).direction.x, Effect.Particle(i).direction.y + (300 * frameTime), 0 ) );
}
//remove particle
for(int i = 0; i < Effect.ParticleAmount(); i++)
{
if(Effect.Particle(i).pos.x < 0 || Effect.Particle(i).pos.x > WIN_W ||
Effect.Particle(i).pos.y < 0 || Effect.Particle(i).pos.y > WIN_H)
{
Effect.deleteParticle(i);
}
}
[/code]
[editline]05:03PM[/editline]
[QUOTE=jA_cOp;24689194]If you're certain it's the memory allocation scheme that is causing it, you can solve it with [url=http://www.cplusplus.com/reference/stl/vector/reserve/]vector::reserve[/url].[/QUOTE]
I'm sure it's the memory allocation "/ thanks, will take a look :)
You should also consider using lists if you don't need random access. A list should be more efficient if you're just iterating over the whole thing all the time.
What does deleteParticle() do?
Also vectors have a .size() function that is less hazardous way to get the number of particles.
[editline]05:12PM[/editline]
Also, accessing individual particles through the Effect class seems overly complex and inefficient.
[QUOTE=esalaka;24689417]You should also consider using lists[/QUOTE]
Lists? :O More info please :D
[QUOTE=RyanDv3;24689440]What does deleteParticle() do? [/QUOTE]
erase it.
particle.erase(particle.begin() + index);
[QUOTE=RyanDv3;24689440]
Also vectors have a .size() function that is less hazardous way to get the number of particles.
[/QUOTE]
I need a function for that, becuase the vector particle is private,
Didn't post that line tho
[code]
int ParticleAmount() { return int( particle.size() ); };
[/code]
[QUOTE=likesoursugar;24689791]Lists? :O More info please :D[/QUOTE]
[url=http://www.cplusplus.com/reference/stl/list/]std::list[/url]
[QUOTE=esalaka;24689417]You should also consider using lists if you don't need random access. A list should be more efficient if you're just iterating over the whole thing all the time.[/QUOTE]
Actually, a vector is (slightly) more efficient for iteration: since all the elements are contiguous in memory, you get fewer cache misses due to better [url=http://en.wikipedia.org/wiki/Locality_of_reference]spatial locality[/url]. A list's strength is being able to efficiently add and remove elements at arbitrary positions.
[QUOTE=esalaka;24689417]You should also consider using lists if you don't need random access. A list should be more efficient if you're just iterating over the whole thing all the time.[/QUOTE]
A list has slower iteration than a vector. It's an extra dereference every time instead of just incrementing. Also vector has better cache locality. I may have missed the negatives that vectors have that would kill performance in this case though, in which case I'm sorry.
EDIT: The delete particle function could be a performance problem here depending how it is coded.
Wait wait wait, I just realized what you are doing. You keep your particles inside a vector, and keep that in a class and made functions to access the vector. This is very bad design. If you are going to to have an effect class, then have the class do the work; meaning, instead of having a loop in main() that moves and deletes particles, have a function in the Effect class that does this. Otherwise, why even have a class? You might as well just keep a vector of particles.
[editline]03:24PM[/editline]
Still shouldn't be killing your fps though, if you posted the entire source I/ we could probably solve your problem very quickly.
(you can just pm it to me if you don't want to post it)
This is your problem:
[code]
sParticle Particle(int Part);
[/code]
Every time you call that function, you create a new copy of the stored particle.
... and you call that function [i]a lot[/i], multiple times even inside of a single loop iteration.
Null's right about that, although I'm not sure if that alone would take your performance down that far.
In the msvc standard library implementation, if you compile in debug mode, operator[] has bounds checking. If that applies to you, that could account for much of the difference in performance. Compiling in release mode doesn't have the bounds checks, and should be much faster.
I can't say if the same is true for other compiler's standard library implemtations.
[QUOTE=bean_xp;24711403]In the msvc standard library implementation, if you compile in debug mode, operator[] has bounds checking. If that applies to you, that could account for much of the difference in performance. Compiling in release mode doesn't have the bounds checks, and should be much faster.
I can't say if the same is true for other compiler's standard library implemtations.[/QUOTE]
With MSVC there's also some defines you have to use to really turn off the checking, even in release. It's pretty stupid.
Sorry, you need to Log In to post a reply to this thread.