E' un bel casino in ANSI C, visto che non puoi usare la keyword ASM...
devi scriverti un moduletto in assembly e compilarlo (ehm... assemblarlo) col MASM32 (scaricalo qui http://www.masm32.com/ ).
Per nostra fortuna il visual studio puo generare codice assembly, che sono andato a modificare un pelino per ottenere questo
codice:
.386P
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS ENDS
$$SYMBOLS SEGMENT BYTE USE32 'DEBSYM'
$$SYMBOLS ENDS
$$TYPES SEGMENT BYTE USE32 'DEBTYP'
$$TYPES ENDS
_TLS SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS ENDS
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
PUBLIC _rdtsc
_TEXT SEGMENT
_rdtsc PROC NEAR
push ebp
mov ebp, esp
sub esp, 192
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-192]
mov ecx, 48
mov eax, -858993460
rep stosd
db 0FH ;masm32 non assembla l'RDTSC
db 31H ;ergo inseriamo direttamente l'opcode in linguaggio
;macchina
pop edi
pop esi
pop ebx
add esp, 192
cmp ebp, esp
mov esp, ebp
pop ebp
ret 0
_rdtsc ENDP
_TEXT ENDS
END
a sto punto il codice (spero ANSI) C sarebbe:
codice:
#include <windows.h>
#include <stdio.h>
UINT64 rdtsc();
double GetCPUFrequency3(unsigned tempo_campionamento)
{
register UINT64 startC, endC;
UINT64 resFrequency, hrCounterFinal, hrCounterTemp = 0, step = 0;
double frequency; // In Hz.
if (QueryPerformanceFrequency( (LARGE_INTEGER*) & resFrequency) == 0) return 0.0;
step = (UINT64) ((double) resFrequency * (tempo_campionamento / 1000.0));
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
startC = rdtsc();
QueryPerformanceCounter((LARGE_INTEGER*) &hrCounterFinal);
hrCounterFinal += step;
while(hrCounterTemp < hrCounterFinal)
QueryPerformanceCounter((LARGE_INTEGER*)&hrCounterTemp);
endC = rdtsc();
SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
frequency = (endC - startC) / (tempo_campionamento / (1000.0)); //10000
return frequency;
}
int main()
{
UINT64 inizio, fine, elapsed;
double CPUFREQ;
inizio = rdtsc();
//fai qualcosa
fine = rdtsc();
elapsed = fine - inizio;
CPUFREQ = GetCPUFrequency3(1000);
printf("%e secondi\n" ,(double)elapsed / CPUFREQ);
return 0;
}
Compila e linka insieme al file .obj generato dal MASM32 dovrebbe funzionare