I'm trying to measure the amount of time for a system call, and I tried using time(0) and gettimeofday() in this program, but whenever I use gettimeofday() it seg faults. I suppose I can just use time(0) but I'd like to know why this is happening. And I know you guys can just look at it and see the problem. Please don't yell at me!
I want to get the time but not save it anywhere.
I've tried every combination of code I can think of but I pasted the simplest version here. I'm new to C and Linux. I look at the .stackdump file but it's pretty meaningless to me.
GetRDTSC is in util.h and it does rdtsc(), as one might expect. Now it's set to 10 iterations but later the loop will run 1000 times, without printf.
#include <stdio.h>
#include <time.h>
#include "util.h"
int main() {
    int i;
    uint64_t cycles[10];
    for (i = 0; i < 10; ++i) {
         // get initial cycles
         uint64_t init = GetRDTSC();
         gettimeofday(); // <== time(0) will work here without a seg fault.
         // get cycles after
         uint64_t after = GetRDTSC();   
         // save cycles for each operation in an array
         cycles[i] = after - init;
         printf("%i\n", (int)(cycles[i]));
    }  
}