I'm trying to compare function call using the clock_gettime but I'm getting weird results.
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
int myppid(){
int myarray[] = {1,2};
return(myarray[1]);
}
main(int argc, char **argv){
uint64_t diff,diff1,diff2;
struct timespec start,end;
int billion = 1000000000;
int i;
/*** for loop to find average overhead ***/
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++); //waste time
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed time of empty for loop: %llu nanoseconds\n", (long long unsigned int) diff);
/*** myppid to find average overhead of a local function ****/
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++) //waste time
{myppid();}
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff1 = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
//diff1 = diff1 - diff;
printf("elapsed time of for loop of myppid: %llu nanoseconds\n", (long long unsigned int) diff1);
// getppid.c to find average overhead of the system call
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++) //waste time
{getppid();}
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff2 = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
//diff2 = diff2 - diff1;
printf("elapsed time of for loop of getppid: %llu nanoseconds\n", (long long unsigned int) diff2);
exit(0);
}
I'm excepting to get increasing clock numbers in nanoseconds but I get:
elapsed time of empty for loop: 421 nanoseconds
elapsed time of for loop of myppid: 160 nanoseconds
elapsed time of for loop of getppid: 195 nanoseconds
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
int myppid(){
int myarray[] = {1,2};
return(myarray[1]);
}
main(int argc, char **argv){
uint64_t diff,diff1,diff2;
struct timespec start,end;
int billion = 1000000000;
int i;
// getppid.c to find average overhead of the system call
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++) //waste time
{getppid();}
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff2 = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
//diff2 = diff2 - diff1;
printf("elapsed time of for loop of getppid: %llu nanoseconds\n", (long long unsigned int) diff2);
/*** myppid to find average overhead of a local function ****/
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++) //waste time
{myppid();}
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff1 = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
//diff1 = diff1 - diff;
printf("elapsed time of for loop of myppid: %llu nanoseconds\n", (long long unsigned int) diff1);
/*** for loop to find average overhead ***/
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++); //waste time
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed time of empty for loop: %llu nanoseconds\n", (long long unsigned int) diff);
exit(0);
}
Surprisingly this code will also give you a decreasing order of time values. But its not so surprising if you visit this link . Its a compiler optimization technique.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.