Skip to content

Instantly share code, notes, and snippets.

@annezao
Last active August 31, 2016 02:42
Show Gist options
  • Save annezao/adf352ae2ed7cc4bdab32d68efd9e7be to your computer and use it in GitHub Desktop.
Save annezao/adf352ae2ed7cc4bdab32d68efd9e7be to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <unistd.h>
unsigned high_i, low_i, high_f, low_f;
uint64_t start, end;
int main()
{
for(int i = 0; i < 5; i++){
asm(".intel_syntax");
asm("cpuid");
asm("rdtsc"); // a função A instrução RDTSC lê o número de ciclos de clock, após o retorno de informações do processador
// com a chamada da instrução cpuid, a instrução RDTSC retorna 64 bits, logo,
// o valor mais significativo fica no registrador EDX e o menos significativo no EAX;
asm("mov high_i, %edx"); //mais significativo inicial
asm("mov low_i, %eax"); //menos significativo inicial
asm("cpuid");
asm("rdtsc");
asm("mov high_f, %edx"); //mais significativo final
asm("mov low_f, %eax"); //menos significativo final
asm(".att_syntax");
printf("Clocks iniciais : %llu %llu\n", high_i, low_i);
printf("Clocks finais : %llu %llu\n", high_f, low_f);
start = ( ((uint64_t)high_i << 32) | low_i ); // equivale à ((high_i * pow(2,32)) + low_i);
end = ( ((uint64_t)high_f << 32) | low_f ); // equivale à ((high_f * pow(2,32)) + low_f);
printf("Comeco : %llu \n", start);
printf("Fim : %llu \n", end);
printf("Ciclos de clock : %llu \n\n", end - start);
printf("Tempo : %llu \n\n", (1/end-start));
sleep(2);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment