Friday, July 22, 2005

line width: DC pen optimization

Don't re-select the DC pen when it's already selected: the GetStockObject and SelectObject calls are considerably more expensive than the test that avoids them.

Benchmark comparing wide-line version to original (default patch, total time spent in Draw function, 1000 calls) reveals that the wide-line version takes 0.06% longer:

old way:
1.3425
1.3459
1.3457
1.3393
1.3419
1.3411
1.3449
AVG = 1.3430

new way:
1.3433
1.3453
1.3428
1.3405
1.3467
1.3431
1.3456
AVG = 1.3439

#include "benchmark.h"
#define MAXSAMPS 1000
double samp[MAXSAMPS];
int samps;
bool done;
void CWhorldView::Draw(HDC dc)
{
Benchmark b;

...

if (samps < MAXSAMPS) {
samp[samps++] = b.Elapsed();
} else {
if (!done) {
FILE *fp = fopen("test.txt", "w");
double sum = 0;
for (int i = 0; i < samps; i++) {
fprintf(fp, "%f\n", samp[i]);
sum += samp[i];
}
fclose(fp);
done = 1;
CString s;
s.Format("%f", sum);
AfxMessageBox(s);
}
}

another test, using Pinwheel but with # sides set to 20 and no LFO, 2500 samples

new 21.6328
old 21.5634
0.32% slower

again:
new 21.6226
old 21.5317
0.42% slower

No comments: