Erratic RDSTC with Direct3D

I'm having some very strange problems when trying to measure the

performance

of Direct3D operations. My code is as follows:

// funny block: timing of the loop below depends on the number of

assignment statements here

c_0 = 0;

.....

.....

.....

.....

c_0 = 0;

if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )

{

t0 = tsc();

// I want to time this loop here

for (c_1 = 0; c_1 < loop_total; c_1 ++){}

t1 = tsc();

total_cycles_elapsed = t1-t0;

sprintf( buffer, "\naverage cycles_elapsed = %lf,

total_cycles_elapsed = %lf",

total_cycles_elapsed/double (loop_total), total_cycles_elapsed);

OutputDebugString (buffer);

m_pd3dDevice->EndScene();

Quote:

}

Here's the funny part: if I have 8 statements in the "funny block",

such as

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

the timing output would be as follows:

average cycles_elapsed = 5.167622, total_cycles_elapsed =

516762208.000000

average cycles_elapsed = 5.198122, total_cycles_elapsed =

519812160.000000

average cycles_elapsed = 5.180695, total_cycles_elapsed =

518069504.000000

average cycles_elapsed = 5.175938, total_cycles_elapsed =

517593760.000000

average cycles_elapsed = 5.252802, total_cycles_elapsed =

525280192.000000

average cycles_elapsed = 5.117760, total_cycles_elapsed =

511775968.000000

average cycles_elapsed = 5.129462, total_cycles_elapsed =

512946240.000000

average cycles_elapsed = 5.081827, total_cycles_elapsed =

508182688.000000

............................

But if I have 9 statements in the "funny block", such as

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

c_0 = 0;

the timing goes up significantly:

average cycles_elapsed = 7.591117, total_cycles_elapsed =

759111744.000000

average cycles_elapsed = 7.616155, total_cycles_elapsed =

761615488.000000

average cycles_elapsed = 7.573227, total_cycles_elapsed =

757322688.000000

average cycles_elapsed = 7.605620, total_cycles_elapsed =

760562048.000000

average cycles_elapsed = 7.606230, total_cycles_elapsed =

760623040.000000

.............................

I'm totally flabbergasted, to say the least, by this kind of totally

inexplicable behavior. I'm sure it's not a problem with alignment since

as soon as I move the loop outside of the BeginScene() /EndScene()

block, the timing is always between 5.0 and 5.2.

PS. Here's my tsc ()

double tsc ()

{

unsigned __int32

cycles_high,

cycles_low;

__int64 current_cycles;

_asm

{

CPUID

pushad

RDTSC

mov cycles_high, edx

mov cycles_low, eax

popad

Quote:

}

current_cycles = ((unsigned __int64) cycles_high << 32) |

cycles_low;

return (double (current_cycles));

Quote:

}

Sent via Deja.com http://www.*-*-*.com/

Before you buy.