timer - Why does times give me the same output after sleep(5) in C -



timer - Why does times give me the same output after sleep(5) in C -

i have problem want measure time of function. phone call timer @ origin , @ end of function returns same value though when phone call sleep(5).

here code:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/times.h> void function_to_time(void); int main(void) { double clockticks, cticks; clock_t tcend, tcstart; struct tms tmend, tmstart; if ((clockticks = (double) sysconf(_sc_clk_tck)) == -1) { perror("failed determine clock ticks per second"); homecoming 1; } printf("the number of ticks per sec %f\n", clockticks); if (clockticks == 0) { fprintf(stderr, "the number of ticks per sec invalid\n"); homecoming 1; } if ((tcstart = times(&tmstart)) == -1) { perror("failed start time"); homecoming 1; } function_to_time(); if ((tcend = times(&tmend)) == -1) { perror("failed end times"); homecoming 1; } cticks = tmend.tms_utime + tmend.tms_stime - tmstart.tms_utime - tmstart.tms_stime; printf("total cpu time operation %f seconds\n", cticks/clockticks); if ((tcend <= tcstart) || (tcend < 0) || (tcstart < 0)) { fprintf(stderr, "tick time wrapped, couldn't calculate fraction\n"); homecoming 1; } printf("fraction of cpu time used %f\n", cticks/(tcend - tcstart)); homecoming 0; } void function_to_time() { sleep(5); }

please note have utilize timer function. i'm using mac os x 10.10 , in virtual machine ubuntu 14.04 on macbook pro.

thanks, best regards armin

because times doesn't give millisecond resolution. gives times in increments of "clk_tck's of second"

https://developer.apple.com/library/mac/documentation/darwin/reference/manpages/man3/times.3.html

edit:

@armin, function have run more 1/clk_tck seconds. after closer look, think @jonathanleffler has more right. the fields of struct returned apply straight calling process listed as:

tms_utime //the cpu time charged execution of user instructions. tms_stime //the cpu time charged execution scheme on behalf of process.

so it's different wall-clock style timings i'm used using. process have actively running (not sleeping) more 1/clk_tck seconds between calls times before see tick.

c timer sleep

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -