Created
February 14, 2013 14:03
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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