Skip to content

Instantly share code, notes, and snippets.

@fser
Created February 14, 2013 14:03
Show Gist options
  • Save fser/4953037 to your computer and use it in GitHub Desktop.
Save fser/4953037 to your computer and use it in GitHub Desktop.
How much does a simple syscall (such as `getpid`) cost in CPU cycle?
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
uint64_t rdtsc();
#ifdef __i386
__inline__ uint64_t rdtsc() {
uint64_t x;
__asm__ volatile ("rdtsc" : "=A" (x));
return x;
}
#elif defined __amd64
__inline__ uint64_t rdtsc() {
uint64_t a, d;
__asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
return (uint64_t)(d<<32) | a;
}
#endif
int main()
{
uint64_t begin = rdtsc();
getpid();
uint64_t end = rdtsc();
printf("begin: %llu end: %llu duration: %llu\n", begin, end, end-begin);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment