Sign in to follow this  

Performance std::Vector Iterator vs integer

This topic is 3557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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

This topic is 3557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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