0

I'm only allowed to use this function and I'm trying to figure out a way to calculate time elapsed, any ideas? I'm confused..

# include <sys/types.h>
# include <sys/time.h>
# include <stdio.h>
# include <unistd.h>


int main(int argc, char* argv[])
{
    struct timeval now;
    struct timeval start;
    int t_now;
    int t_stop;
    int t_start;
    int i;

    gettimeofday(&start, NULL);
    t_start = start.tv_usec / 1000;
    t_stop = t_start + 200;

    while (42)
    {
        usleep(2000);
        gettimeofday(&now, NULL);
        t_now = now.tv_usec / 1000;
        if (t_now - t_stop > -1)
        {
            break;
        }
        printf ("%d\n", t_now);
        usleep(2000);
    }
    return 0;
}

I was doing this but it will keep looping, and won't stop..

Abrar
  • 3
  • 5
  • 1
    call it once at the start, remember the tv_sec value, when finished call it again, subtract new tv_sec from old – pm100 Feb 28 '22 at 23:28
  • @pm100 No this is impossible you can't do this cuz gettimeofday will return you the current time .. I mean in each minute it will counts the minutes back it's not constant wait check this link https://blog.habets.se/2010/09/gettimeofday-should-never-be-used-to-measure-time.html – Abrar Feb 28 '22 at 23:40
  • that blog article is not very good, its confusing and misleading. You asked how to measure elapsed time using gettimeofday, I told you how to do it. What I said will work, if you need more than 1 second accuracy then take into account the tv_usec value too – pm100 Feb 28 '22 at 23:48
  • Put code in the question, not comments. – Barmar Feb 28 '22 at 23:57
  • A blog that says not to use `gettimeofday()` is not very helpful if you're *required* to use it. Your assignment doesn't care about the possible problems (which aren't really as significant as he suggests). – Barmar Feb 28 '22 at 23:58

1 Answers1

0

this

t_start = start.tv_usec / 1000;

isnt doing what you expect. tv_usec only has fractions of a second, you need

long long msec = (start.tv_sec * 1000) + start.tv_usec/1000;

to get total msec

pm100
  • 48,078
  • 23
  • 82
  • 145
  • what is msec? I thought tv_sec and tv_usec are the same value one in microseconds and the other is just seconds.. – Abrar Mar 01 '22 at 00:21
  • @AbrarX nope, secs is the number of seconds, then add usec to get the fractions of second. I show you how to calculate milliseconds, by multiplying seconds by 1000 and adding microseconds / 1000 – pm100 Mar 01 '22 at 00:23
  • okkkaaayyyy I do understand now thank you so much I was confused and I didn't find a website that could explain what sec and usec I thought they are the same .. – Abrar Mar 01 '22 at 00:35
  • @Abrar use your debugger, i did and i got this `$1 = {tv_sec = 1646094894, tv_usec = 776419}`, its pretty obvious that 776419 is not the time in microseconds! – pm100 Mar 01 '22 at 00:35
  • oh wait I didn't print them out together .. I misunderstood the manual .. okay I will use the debugger – Abrar Mar 01 '22 at 00:49