Thread: Time measurement returns negative value

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Time measurement returns negative value

    I am using the first method, listed here.

    I want to take the time measurement of a construction of a tree, which contains about 2841482 nodes (inner and leaves).

    Here it is:
    Code:
    struct timespec start, end;
    clock_gettime(CLOCK_MONOTONIC, &start);
    
    Tree t(...);
        
    clock_gettime(CLOCK_MONOTONIC, &end);
    
    int64_t time;
    time = timespecDiff(&end, &start);
    std::cout<<"Time: " << time << " ns\n";
    which gives me always a negative value, like -13481628 ns.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first thing would be to check the return result of clock_gettime()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not know exactly what native type is used for timeA_p->tv_sec, but timeA_p->tv_nsec is long and I do not see a cast to int_64 inside
    Code:
    int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
    {
      return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
               ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
    }
    So I suppose there is mathematical overflow... - you need to add cast to int64_t inside this expression
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    timeA_p->tv_sec * 1000000000
    It might overflow.

    Next time include all functions inside the post, so that others don't have to see irrelevant content like that.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Are struct timespec members declared as type int64_t? If not, then the code in timespecDiff() could be the issue.

    update - previous posts already mentioned this.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Already tried that Salem, but I got this
    Code:
    main.cpp:54: error: no match for 'operator>>' in '(+std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+(&std::cout)->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](end.timespec::tv_sec))), ((const char*)" ")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](start.timespec::tv_sec) >> " \n"'
    Vart, where exactly do you suggest me adding int64_t to?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Shouldn't your code be:
    Code:
    int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
    {
      return (((int64_t)timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
             (((int64_t)timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by std10093 View Post
    I am using the first method, listed here.

    I want to take the time measurement of a construction of a tree, which contains about 2841482 nodes (inner and leaves).

    Here it is:
    Code:
    struct timespec start, end;
    clock_gettime(CLOCK_MONOTONIC, &start);
    
    Tree t(...);
        
    clock_gettime(CLOCK_MONOTONIC, &end);
    
    int64_t time;
    time = timespecDiff(&end, &start);
    std::cout<<"Time: " << time << " ns\n";
    which gives me always a negative value, like -13481628 ns.
    Not sure, but what is sizeof( time_t ) on your system? If it's say 4 you'd get an integer overflow in timespecDiff.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    That was the case! Thank you!!! Poland guy you are right about the link.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It should be noted that there are instances on multi-core CPUs where time calculations can return negative values. This usually happens with the QueryPerformanceCounter API calls. The simple answer is to clamp the result at 0 even though this does not result in an accurate timer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time measurement and sound
    By mewatC in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2006, 01:50 PM
  2. time.h: time returns different times
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-24-2005, 03:38 AM
  3. time measurement using clock() and time()
    By kuhnmi in forum C++ Programming
    Replies: 1
    Last Post: 08-13-2004, 06:32 AM
  4. Accurate time measurement
    By Brian in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2003, 02:00 AM