Sign in to follow this  
fooman_69

Which way is faster?

Recommended Posts

fooman_69    174
Hiya, Which is faster? I think the 2nd one would be, but my FPS doesn't show much difference.
//Draw players
	glLoadIdentity();
	glBegin(GL_QUADS);
		//player 1
		glTexCoord2f(ent1->GetImage().left, ent1->GetImage().bottom);	glVertex2f((float)(p1_x * TILE_SIZE), (float)(p1_y * TILE_SIZE));							// Bottom Left Of The Texture and Quad
		glTexCoord2f(ent1->GetImage().right, ent1->GetImage().bottom);	glVertex2f((float)(p1_x * TILE_SIZE + TILE_SIZE), (float)(p1_y * TILE_SIZE));				// Bottom Right Of The Texture and Quad
		glTexCoord2f(ent1->GetImage().right, ent1->GetImage().top);		glVertex2f((float)(p1_x * TILE_SIZE + TILE_SIZE), (float)(p1_y * TILE_SIZE - TILE_SIZE));	// Top Right Of The Texture and Quad
		glTexCoord2f(ent1->GetImage().left, ent1->GetImage().top);		glVertex2f((float)(p1_x * TILE_SIZE), (float)(p1_y * TILE_SIZE - TILE_SIZE));				// Top left of the texture and quad

		//player 2
		glTexCoord2f(ent2->GetImage().left, ent2->GetImage().bottom);	glVertex2f((float)(p2_x * TILE_SIZE), (float)(p2_y * TILE_SIZE));							// Bottom Left Of The Texture and Quad
		glTexCoord2f(ent2->GetImage().right, ent2->GetImage().bottom);	glVertex2f((float)(p2_x * TILE_SIZE + TILE_SIZE), (float)(p2_y * TILE_SIZE));				// Bottom Right Of The Texture and Quad
		glTexCoord2f(ent2->GetImage().right, ent2->GetImage().top);		glVertex2f((float)(p2_x * TILE_SIZE + TILE_SIZE), (float)(p2_y * TILE_SIZE - TILE_SIZE));	// Top Right Of The Texture and Quad
		glTexCoord2f(ent2->GetImage().left, ent2->GetImage().top);		glVertex2f((float)(p2_x * TILE_SIZE), (float)(p2_y * TILE_SIZE - TILE_SIZE));				// Top left of the texture and quad
	glEnd();
OR
	//Draw players
	Sprite ent1Spr = ent1->GetImage();
	Sprite ent2Spr = ent2->GetImage();

	glLoadIdentity();
	glBegin(GL_QUADS);
		//player 1
		glTexCoord2f(ent1Spr.left, ent1Spr.bottom);		glVertex2f((float)(p1_x * TILE_SIZE), (float)(p1_y * TILE_SIZE));							// Bottom Left Of The Texture and Quad
		glTexCoord2f(ent1Spr.right, ent1Spr.bottom);	glVertex2f((float)(p1_x * TILE_SIZE + TILE_SIZE), (float)(p1_y * TILE_SIZE));				// Bottom Right Of The Texture and Quad
		glTexCoord2f(ent1Spr.right, ent1Spr.top);		glVertex2f((float)(p1_x * TILE_SIZE + TILE_SIZE), (float)(p1_y * TILE_SIZE - TILE_SIZE));	// Top Right Of The Texture and Quad
		glTexCoord2f(ent1Spr.left, ent1Spr.top);		glVertex2f((float)(p1_x * TILE_SIZE), (float)(p1_y * TILE_SIZE - TILE_SIZE));				// Top left of the texture and quad

		//player 2
		glTexCoord2f(ent2Spr.left, ent2Spr.bottom);		glVertex2f((float)(p2_x * TILE_SIZE), (float)(p2_y * TILE_SIZE));							// Bottom Left Of The Texture and Quad
		glTexCoord2f(ent2Spr.right, ent2Spr.bottom);	glVertex2f((float)(p2_x * TILE_SIZE + TILE_SIZE), (float)(p2_y * TILE_SIZE));				// Bottom Right Of The Texture and Quad
		glTexCoord2f(ent2Spr.right, ent2Spr.top);		glVertex2f((float)(p2_x * TILE_SIZE + TILE_SIZE), (float)(p2_y * TILE_SIZE - TILE_SIZE));	// Top Right Of The Texture and Quad
		glTexCoord2f(ent2Spr.left, ent2Spr.top);		glVertex2f((float)(p2_x * TILE_SIZE), (float)(p2_y * TILE_SIZE - TILE_SIZE));				// Top left of the texture and quad
	glEnd();

Share this post


Link to post
Share on other sites
Promit    13246
Not to mention that optimization is completely useless in the face of the fact that you're using glBegin/End/Vertex/etc.

Share this post


Link to post
Share on other sites
Binomine    611
It's pointless to optimize Immediate OpenGL. Step into Vertex Arrays. (Do a search on Vertex Arrays on that page and read up on it).

Actually, I take that back, it's not pointless to optimize any OpenGL, but optimizing OpenGL is more about sorting data to minimize state changes rather than finding superfast commands.

Whenever you glEnable, glDisable, glBind, glBegin and glEnd, you take a performance hit. Getting the OpenGL state machine in that sweet spot where you put the most out on the screen while call those commands the least is your optimizing dream.

Share this post


Link to post
Share on other sites
fooman_69    174
Thnaks for the replies.
I only asked because it was beat into my throughout college that optimization is key. I figured 10 calls to a class method was more costly than creating two temp variables once.

I will do my best to optimize the gl calls however.
If I am drawing quads throughout the Render method, I should only ever need one glBegin/glEnd call right?

Share this post


Link to post
Share on other sites
Squirm    481
It was beat into you that optimization is the key?

I recommend a long period of recuperation, possibly involving self flagelation, to beat it back out again...

"Premature optimization is the root of all evil."

"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity."

"There are three rules of optimization:
1) Don't optimize.
2) Don't optimize yet.
3) Don't optimize unless a profiler specifically tells you to do so."

"80% of your code is run 20% of the time" (and optimizing that 80% is therefore pointless).

And many more quotes... and threads... both in gamedev and elsewhere... I was thinking all this even as I read your subject...



Share this post


Link to post
Share on other sites
fooman_69    174
Perfectly understood, and I wish you were there in class as they taught us to code in this fashion. We could have used another person questioning their optimization lessons.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:

"There are three rules of optimization:
1) Don't optimize.
2) Don't optimize yet.
3) Don't optimize unless a profiler specifically tells you to do so."


The two rules of optimization:
1) Don't optimize
2) ( * For Experts Only * ) Don't optimize yet

Share this post


Link to post
Share on other sites
markr    1692
"Which way is faster?" pre-supposes:

1. It matters (i.e. there is a significant difference)
2. Runtime performance is the most important thing to you

Personally, I'd say, developer performance is *FAR* more important than runtime performance. Developer performance can be optimised by doing things in the simplest way that works, and not wasting time posting on gamdev.net

"Which is faster? x = x +1 or x ++ or ++x ????"

Mark

Share this post


Link to post
Share on other sites
Binomine    611
Quote:
Original post by fooman_69
Thnaks for the replies.
I only asked because it was beat into my throughout college that optimization is key.
Optimization is the key, but optimization is more about finding the best algorthm and less about finding the fastest implementation of an algorthm.
Quote:
I figured 10 calls to a class method was more costly than creating two temp variables once.
True, in unoptimizing compiler land, the latter would be faster than the former. However, most likely your compiler is doing automatically what you're forcing it to do by hand.

Also, pointers would probably be slight faster in unoptimzed compiler land as well, since you're making a copy of the data to simply reference it.
Quote:
I will do my best to optimize the gl calls however.
If I am drawing quads throughout the Render method, I should only ever need one glBegin/glEnd call right?
I haven't seen your Render method nor how you're texturing these quads, so I cannot say with any certiany.

You are only allowed to call:
glVertex*, glColor*, glIndex*, glNormal*, glTexCoord*, glEvalCoord, glEvalPoint, glArrayElement, glMatrial
within a glBegin/glEnd statement. If you need to use glBindTexture, for example, you'll have to step outside of the glBegin/glEnd.


However, if you're worried about speed, you shouldn't be using glBegin/glEnd anyways. VARs(glDrawElements/glDrawArrays) are your friend.(VARs calls a pseduo-glBegin/glEnd for you.)
~~
Premature Optimization is the worse thing you can do.
Optimization is great, but at the algorthmic level.

Carmack doesn't sit around all day saying, "I should switch this while() loop for a do...while() loop, it'll make my code 20 bytes longer, but it should remove 2 clock cycles. Sure it's in the Menu, but every optimization counts".

Instead...he sits around all day saying, "My profiler says I spend a lot of time in this code. If I simplify this part of my algorthm, the users bullets would be more inaccurate, but not enough to notice, and I'll decrease the steps in my algorthm, speeding up my engine."

Followed by, "Let's shoot things into Outer Space!"

Share this post


Link to post
Share on other sites
Zahlman    1682
The code is in the "wrong place" anyway (as well as much of the data probably):


// Player class aggregates the whatever ent1/ent2 is, along with the p1/p2 x/y
// i.e. it represents the player's position and "entity"
void Player::draw() {
// You might instead choose to wrap draw() in a glBegin/End block, to make
// sure that the state is correct before drawing (i.e. quads). That would
// indeed be a valid optimization issue - doing things that way makes sure
// that you won't have weird things happen if you try to draw the Player in
// other contexts, but will result in redundant calls to the (considered-slow)
// GL state transition functions.

// bottom-left
glTexCoord2f(image.left, image.bottom);
glVertex2f((float)(x * TILE_SIZE), (float)(y * TILE_SIZE));
// within the object for player1, p1_x and p1_y are stored; similarly player2
// similarly the other three corners
}
// The whole point with the "encapsulation vs optimization problem" is that the
// code belongs next to the data it operates on. If you are worrying about the
// performance of a direct member access vs. the "OO-ness" of a raw accessor -
// then you have failed to understand OO.

// And then in the drawing:
glLoadIdentity();
glBegin(GL_QUADS); // unless encapsulated in draw()
player1.draw();
player2.draw();
glEnd(); // unless encapsulated in draw()

Share this post


Link to post
Share on other sites
kburkhart84    3187
Just to clarify the above, what the posts mean is to optimize BIG things, not little things. Compilers usually optimize many of the little things anyway. For example, take the quads and put them into a display list or vertex array instead of using immediate mode. That gets you a big boost in speed because it is a BIG thing. As said above, with OpenGL, a big optimization is trying to avoid state changes when possible. As in, not going to the extreme, rather when you can batch tris of the same texture, do so, but don't write an extremely long code block to batch them, unless it runs at initialization, not at run time. Try to keep things simple. Then when done, profile the application/game to see where the time is being taken up, look for the bottleneck. Optimize it, (IF NEEDED), then go on with your day. Don't waste time optimizing to see if i++ or ++i is faster. The difference is so liitle that the end user won't notice. And remember, the end user is the one who really cares.

Share this post


Link to post
Share on other sites
fooman_69    174
Seriously, thanks but I was just curious. I'm not sitting here thinking about optimization in terms of 0++ or ++0. I just felt I would ask to see if any of this mattered. Apparently it does not.
btw, I really appreciate the condescending tones of many of the above posts.

Share this post


Link to post
Share on other sites
markr    1692
Ignoring optimisation, from a style point of view, I'd say:


//Draw players
const Sprite & ent1Spr = ent1->GetImage();
glLoadIdentity();
glBegin(GL_QUADS);
//player 1
glTexCoord2f(ent1Spr.left, ent1Spr.bottom

// etc



I suspect that none of the implementations is likely to differ vastly in runtime performance, and even if they do, you probably don't care.

Mark

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