Section C.1. Measure Performance


C.1. Measure Performance

Before you optimize your code, you need a way to measure performance objectively so that you'll know when you've made an improvement.

GLUT provides glutGet( GLUT_ELAPSED_TIME ), which applications can use to obtain platform-independent timing information. It returns an integer count of milliseconds elapsed since the application started. Many platforms, however, provide platform-specific routines for obtaining time information, which might provide more precision. If you choose to use GLUT_ELAPSED_TIME, you'll need to measure several frames to obtain an accurate measurement on such a coarse timer.

Because OpenGL supports a client-server model, OpenGL often buffers commands internally. As a result, some OpenGL commands return to the application before they execute. This behavior can skew performance measurements if you attempt to time such a command. You can block until OpenGL executes all commands by using glFinish(), however. Because glFinish() has a nonzero cost, you'll want to measure several rendered framespossibly hundredsand then use a single glFinish() call before obtaining time information. This amortizes the expense of the glFinish() call over all rendered frames, increasing the accuracy of the timing measurement.


void glFinish( void );


Blocks until all preceding OpenGL commands have been completely executed.

Although many end-users might not know what it means to synchronize buffer swaps to the monitor refresh rate (or vertical sync), as an OpenGL developer, you must disable vertical sync to obtain an accurate performance measurement. Don't measure performance with vertical sync enabled. How you disable it depends on your development platform and graphics card. Some devices control vertical sync with an environment variable; others, with a device-driver control application. Some OpenGL implementations also support an extension to disable vertical sync,[1] which allows your application to disable it programmatically.

[1] GLX_SGI_video_sync is one such extension.

To obtain an accurate measurement of frames rendered per second, instrument your application as shown in the following pseudocode:

 glFinish(); const int start = glutGet( GLUT_ELAPSED_TIME ); Render N frames, where N is large. glFinish(); const int end = glutGet( GLUT_ELAPSED_TIME ); const float fps = (float)( end  start ) / (float)N; 


The larger the value of N (the more frames you include in the timing measurement), the more accurate your result will be, as large values of N amortize the cost of the final glFinish() call and also compensate for the coarse GLUT timing value.

Rather than take timing measurements manually by inserting code to measure the elapsed time, professional software developers often use thirdparty optimization tools, such as Intel's VTune or the GNU gprof profiler. Some OpenGL development tools, such as Apple's OpenGL Profiler, contain both debugging and optimization features.




OpenGL Distilled
OpenGL Distilled
ISBN: 0321336798
EAN: 2147483647
Year: 2007
Pages: 123
Authors: Paul Martz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net