Sign in to follow this  
Ben091986

Performance std::Vector Iterator vs integer

Recommended Posts

Ben091986    108
hi folks, is an iterator faster than an integer? I have an vector with about 300 elements and a little loop every frame. so i coded two versions iterator vs integer, and integer was very much faster. did i make something wrong ?? Maybe i missed some optimization for the iterator or so... They are both completly equal beside the fact, that the first version is faster
//prtcl is a vector<T>
//size of vector 300

//version one 350 FPS
	for(int i = 0;i< maxPart_U;i++)
	{	
		if((*prtcl)[i].time < 0)
		{
			if(run_U)
			{
			
				if(spawnCounter_U > spawnTime_U)
				{
					spawnCounter_U-= spawnTime_U;
					(*prtcl)[i]    = updateCreateParticle(dt);	
				}
				else	 (*prtcl)[i].col = D3DCOLOR_COLORVALUE(0,0,0,0);
			}
			else
			{
				(*prtcl)[i].col = D3DCOLOR_COLORVALUE(0,0,0,0);
			}
			
		}
		else
		{
			effectChangeParticle((*prtcl)[i],dt);
		}
		
	}

//version 2 280FPS
	for(vector<T>::iterator it = prtcl->begin();it != prtcl->end();it++)
	{	
		if((*it).time < 0)
		{
			if(run_U)
			{
			
				if(spawnCounter_U > spawnTime_U)
				{
					spawnCounter_U-= spawnTime_U;
					(*it)    = updateCreateParticle(dt);	
				}
				else	  (*it).col = D3DCOLOR_COLORVALUE(0,0,0,0);
			}
			else
			{
				(*it).col = D3DCOLOR_COLORVALUE(0,0,0,0);
			}
			
		}
		else
		{
			effectChangeParticle((*it),dt);
		}
		
	}

Share this post


Link to post
Share on other sites
SiCrane    11839
Are you timing debug or release mode? Are you using MSVC or another STL implementation with runtime iterator checks?

Share this post


Link to post
Share on other sites
kiome    229
Using VC++ you'll have to define _SECURE_SCL 0 before including the standard headers to disable checked iterators even in a release build.

PS: Profiling for performance in debug mode is pretty much useless.

Share this post


Link to post
Share on other sites
asp_    172
Disabling secure iterator checking only helps if you have a very tight loop with very little branching. Looking at your code you do plenty of branching already so I doubt disabling them will speed anything up.

Share this post


Link to post
Share on other sites
dmatter    4821
Quote:
Original post by Ben091986
In release mode they are both equal. 530 FPS

The iterator is probably fractionally faster but you're unlikely to notice the difference when you're measuring frames per second as an integer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this