Skip to content

Instantly share code, notes, and snippets.



Last active Sep 26, 2020
What would you like to do?
* ctx_time Copyright (C) 2018 Matteo Croce <>
* a tool measure the context switch time in clock cycles
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <x86intrin.h>
int main (int argc, char *argv[])
int i;
unsigned long long min = ULLONG_MAX;
unsigned long long time;
unsigned junk;
for (i = 0; i < 10000000; i++) {
time = __rdtscp(&junk);
/* Can't be used with BSD because raises a SIGSYS which
* consumes ~200 extra clock cycles even if trapped.
* Replace with:
* getuid();
time = __rdtscp(&junk) - time;
if (time < min)
min = time;
printf("ctx: %llu clocks\n", min);
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment