Re: OT: recommended way of timing two pieces of code in C

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> >>>
> >> Note that the accuracy of this depends on several factors, such as how
> >> long the measured interval is compared to the basic unit of timekeeping
> >> (IOW if you want to measure the execution time of a short sequence of
> >> instructions, you need to loop a few million times and divide the
> >> result), and how much overhead is incurred in calling the timing
> >> routines.
> > Btw, this webpage 
> > https://stackoverflow.com/questions/23847588/how-do-i-use-the-functions-setitimer-and-getitimer
> > says that getitimer and setitimer are obsolete and I should be using 
> > timer_gettime() and timer_settime() instead: however, is it possible 
> > to handle a virtual clock with these new functions?
> >
> > Best wishes,
> > Ranjan
> 
> the manp ages for the posix compliant interfaces for timer_settime and 
> timer_gettime
> do not even mention the word virtual; at least not in my fedora 22 manpages.
> Whereas the still valid setitimer and getitimer CAN get the per running 
> process virtual time,
> which will exclude all system time and interrupt handling time.
> There are plenty of examples online for using setitimer() and getitimer().
> 

OK, trying an example:

#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

#define INTERVAL 1        /* number of milliseconds to go off */

int main() {
    double sum = 0;
    struct itimerval initial, updated;

    initial.it_value.tv_sec     = INTERVAL/1000000;
    initial.it_value.tv_usec    = (INTERVAL/1000000) * 1000000;
    initial.it_interval = initial.it_value;

    if (setitimer(ITIMER_VIRTUAL, &initial, NULL) == -1) {
        perror("error calling setitimer()");
        exit(1);
    }

    for (unsigned int i; i < UINT_MAX; i++) {
        sum += 1./i;
    }
    if (getitimer(ITIMER_VIRTUAL, &updated) == -1) {
        perror("error calling getitimer()");
        exit(1);
    }

    printf("Time started = %ld\n; Time taken = %ld\n: Time taken = %ld\n",
           initial.it_value.tv_usec, updated.it_value.tv_usec,
           updated.it_value.tv_usec - initial.it_value.tv_usec);
    return 0;
}

compiled with:

$ gcc -o timer -std=c99 -Wall -pedantic getitimer.c -lrt

However: I always get zero.

$ ./timer 
Time started = 0
; Time taken = 0
: Time taken = 0

What am I doing wrong?

Many thanks,
Ranjan

____________________________________________________________
Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords & protects your account.
Check it out at http://mysecurelogon.com/manager


-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Fedora Code of Conduct: http://fedoraproject.org/code-of-conduct
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux