PDA

View Full Version : Which code is faster?



HanWu
07-07-2003, 11:20 PM
1)
int i=0;

glBegin(GL_POINTS);
for(i=0;i<numOfVertex;i++){
glVertex3fv(&poly[i].x);
}
glEnd();
for(i=0;i<numOfVertex-1;i++){
glBegin(GL_LINES);
glVertex3fv(&poly[i].x);
glVertex3fv(&poly[i+1].x);
glEnd();
}

2)
for(i=0;i<numOfVertex-1;i++){
glBegin(GL_POINTS);
glVertex3fv(&poly[i].x);
glEnd();

glBegin(GL_LINES);
glVertex3fv(&poly[i].x);
glVertex3fv(&poly[i+1].x);
glEnd();
}
glBegin(GL_POINTS);
glVertex3fv(&poly[i].x);
glEnd();

At first i taught the 2nd one should be faster because only one loop. But when i try both of the code, the first one can render faster than the 2nd one. Does anyone know the reason?

errno
07-07-2003, 11:31 PM
the first code is faster because you call glBegin(...)/glEnd() less than in the second one.

note that this code is faster:

glBegin(GL_POINTS);
for(i=0;i<numOfVertex;i++)
{
glVertex3fv(&poly[i].x);
}
glEnd();
glBegin(GL_LINE_STRIP);
for(i=0;i<numOfVertex;i++)
{
glVertex3fv(&poly[i].x);
}
glEnd();