timer in milliseconds

Discussion in 'C Programming' started by S_at_work, Oct 27, 2003.

  1. S_at_work

    S_at_work Guest

    hello,



    can anybody give me a piece of code , or a hint , how i can recieve the
    time i need for connect() to another host in milliseconds.



    My problem is i want test the performance of ipsec, so i need the
    time in milliseconds, how long it takes to connect to another host
    over ipsec.



    1) i tried clock() but clock() don't work, because connect() don't use
    cpu time like sleep(), so i always get 0 milliseconds, but the
    connect takes about 2 seconds (i test this with a sniffer) .



    is the any way to get the time in milliseconds ????



    thanks a lot and best regards, stefan


    --
    Posted via http://dbforums.com
    S_at_work, Oct 27, 2003
    #1
    1. Advertising

  2. S_at_work

    Dan Pop Guest

    In <> S_at_work <> writes:


    >can anybody give me a piece of code , or a hint , how i can recieve the
    >time i need for connect() to another host in milliseconds.


    If you post this question to a newsgroup dedicated to programming on
    your platform, I'm reasonably sure someone can. There is no portable
    solution to your problem, the best you can get from standard C for
    measuring real time intervals is time() and it typically works with
    second resolution, i.e. three orders of magnitude worse than what you
    need.

    >1) i tried clock() but clock() don't work, because connect() don't use
    > cpu time like sleep(), so i always get 0 milliseconds, but the


    Where did you get the idea that sleep() uses CPU time from?

    > connect takes about 2 seconds (i test this with a sniffer) .


    Yup, forget about clock(), it's not what you need, regardless of any
    resolution issues: it does NOT measure real time intervals.

    >is the any way to get the time in milliseconds ????


    If you're on a Unix platform and if microseconds are good enough, check
    gettimeofday(2).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 27, 2003
    #2
    1. Advertising

  3. "S_at_work" <> wrote in message
    news:...

    > can anybody give me a piece of code , or a hint , how i can recieve the
    > time i need for connect() to another host in milliseconds.


    Many processors now have a register that counts clock cycles. If yours
    does, this probably will do what you want. (You also need to know the clock
    rate, though.)

    It takes a two instruction assembly program on x86 (off topic) , for
    example.

    -- glen
    Glen Herrmannsfeldt, Oct 27, 2003
    #3
  4. (Dan Pop) writes:

    > In <> S_at_work
    > <> writes:

    [...]
    > >1) i tried clock() but clock() don't work, because connect() don't use
    > > cpu time like sleep(), so i always get 0 milliseconds, but the

    >
    > Where did you get the idea that sleep() uses CPU time from?


    I think he meant that connect(), like sleep(), doesn't use CPU time.
    (It uses some, of course, but not enough to make it useful to measure
    it.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Oct 27, 2003
    #4
  5. S_at_work

    Randy Howard Guest

    In article <shenb.42505$Tr4.87514@attbi_s03>, says...
    > Many processors now have a register that counts clock cycles. If yours
    > does, this probably will do what you want. (You also need to know the clock
    > rate, though.)
    >
    > It takes a two instruction assembly program on x86 (off topic) , for
    > example.


    Please don't post this OT stuff, especially since it is likely to result
    in bad results. For example, the solution using rdtsc for x86 is horribly
    broken on SMP systems.

    --
    Randy Howard _o
    2reply remove FOOBAR \<,
    ______________________()/ ()______________________________________________
    SCO Spam-magnet:
    Randy Howard, Oct 28, 2003
    #5
  6. "Randy Howard" <> wrote in message
    news:...
    > In article <shenb.42505$Tr4.87514@attbi_s03>, says...
    > > Many processors now have a register that counts clock cycles. If yours
    > > does, this probably will do what you want. (You also need to know the

    clock
    > > rate, though.)
    > >
    > > It takes a two instruction assembly program on x86 (off topic) , for
    > > example.

    >
    > Please don't post this OT stuff, especially since it is likely to result
    > in bad results. For example, the solution using rdtsc for x86 is horribly
    > broken on SMP systems.


    Certainly it us up to the user to understand the system in use.

    I have used rdtsc on a SMP system, without considering the problems that it
    might cause. As well as I remember it, it worked fine, but I can see that
    it could cause problems. I think the results will still be better than
    clock(), though.

    -- glen
    Glen Herrmannsfeldt, Oct 28, 2003
    #6
  7. S_at_work

    S_at_work Guest

    thanks for help now it works

    here is the code







    #include <stdio.h>

    #include <time.h>



    #define MAIN

    //#define DEBUG



    float GetTimeDelta(struct timeval *time1){





    float timereturn;

    struct timeval time2;

    time2.tv_usec=0;

    time2.tv_sec=0;





    if(time1->tv_usec==NULL&&time1->tv_sec==NULL ){

    timereturn = 0.0;

    gettimeofday(time1,NULL);

    }

    else{

    gettimeofday(&time2,NULL);

    if(time1->tv_usec > time2.tv_usec){

    timereturn =((1000000.0+(time2.tv_usec-time1-
    >tv_usec))/1000000.0);


    time2.tv_sec-=1;

    // printf(" milliseconds 1>2:%f\n",timereturn);

    }

    else{

    timereturn =(time2.tv_usec-time1->tv_usec)/1000000.0;

    //printf(" milliseconds 2>1:%f\n",timereturn);

    }

    timereturn+=(time2.tv_sec -time1->tv_sec);



    }

    #ifdef DEBUG

    printf("time1 seconds:%d\n",time1->tv_sec);

    printf("time1 milliseconds:%d\n",time1->tv_usec);



    printf("time2 seconds:%d\n",time2.tv_sec);

    printf("time2 milliseconds:%d\n",time2.tv_usec);



    return timereturn;

    #endif DEBUG

    }





    #ifdef MAIN

    int main()

    {

    struct timeval time;

    time.tv_sec =0;

    time.tv_usec=0;

    int i=100000;

    int a=0;

    printf("start time %f\n",GetTimeDelta(&time));



    //time consuming

    sleep(4);

    while(i--)

    a= i%12;

    //end time consuming



    printf("end time %f\n",GetTimeDelta(&time));





    }

    #endif


    --
    Posted via http://dbforums.com
    S_at_work, Oct 28, 2003
    #7
  8. S_at_work

    Dan Pop Guest

    In <2Unnb.46121$Tr4.103510@attbi_s03> "Glen Herrmannsfeldt" <> writes:


    >"Randy Howard" <> wrote in message
    >news:...
    >> In article <shenb.42505$Tr4.87514@attbi_s03>, says...
    >> > Many processors now have a register that counts clock cycles. If yours
    >> > does, this probably will do what you want. (You also need to know the

    >clock
    >> > rate, though.)
    >> >
    >> > It takes a two instruction assembly program on x86 (off topic) , for
    >> > example.

    >>
    >> Please don't post this OT stuff, especially since it is likely to result
    >> in bad results. For example, the solution using rdtsc for x86 is horribly
    >> broken on SMP systems.

    >
    >Certainly it us up to the user to understand the system in use.
    >
    >I have used rdtsc on a SMP system, without considering the problems that it
    >might cause. As well as I remember it, it worked fine, but I can see that
    >it could cause problems. I think the results will still be better than
    >clock(), though.


    If the solution has to be platform-specific, anyway, why not use a *clean*
    solution provided by the implementation, as an extension? Especially
    since the OP only needs millisecond resolution.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 28, 2003
    #8
  9. S_at_work

    Dan Pop Guest

    In <> S_at_work <> writes:


    >thanks for help now it works
    >
    >here is the code


    Why did you feel compelled to post non-portable C code to this newsgroup?

    BTW, the *correct* usage of gettimeofday requires the inclusion of
    <sys/time.h>.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 28, 2003
    #9
  10. S_at_work

    Randy Howard Guest

    OT: Re: timer in milliseconds

    In article <2Unnb.46121$Tr4.103510@attbi_s03>,
    says...
    > Certainly it us up to the user to understand the system in use.


    Absolutely.

    > I have used rdtsc on a SMP system, without considering the problems that it
    > might cause. As well as I remember it, it worked fine, but I can see that
    > it could cause problems.


    The problem is that the tsc data is not synched between CPUs, meaning that
    your results are only accurate as long as your process (or thread) is
    pinned to a specific CPU. That's usually not the case (nor ideal).


    --
    Randy Howard _o
    2reply remove FOOBAR \<,
    ______________________()/ ()______________________________________________
    SCO Spam-magnet:
    Randy Howard, Oct 28, 2003
    #10
  11. Re: Re: timer in milliseconds

    "Randy Howard" <> wrote in message
    news:...
    > In article <2Unnb.46121$Tr4.103510@attbi_s03>,
    > says...
    > > Certainly it us up to the user to understand the system in use.

    >
    > Absolutely.
    >
    > > I have used rdtsc on a SMP system, without considering the problems that

    it
    > > might cause. As well as I remember it, it worked fine, but I can see

    that
    > > it could cause problems.

    >
    > The problem is that the tsc data is not synched between CPUs, meaning that
    > your results are only accurate as long as your process (or thread) is
    > pinned to a specific CPU. That's usually not the case (nor ideal).


    I have a post to comp.sys.intel to see if anyone there knows. I know TSC
    is zero at reset, and if all leave reset at the same time, they may be
    synchronized. I will see if anyone answers there.

    The one time I did it on a four way SMP machine was in Java, using a JVM
    native method, which means C. I took a simple C program, compiled it
    with -S, modified the result with an RDTSC instruction, assembled it, and it
    pretty much worked. I didn't need nanosecond resolution, but millisecond
    may not have been good enough. There are enough problems with the way Java
    native method calls not to worry too much about the SMP problem.

    I will see what they say on comp.sys.intel, though. I have a dual 350MHz P2
    system at home, but I never tried it on that one.

    -- glen
    Glen Herrmannsfeldt, Oct 28, 2003
    #11
  12. S_at_work

    Randy Howard Guest

    Re: Re: timer in milliseconds

    In article <hyAnb.51086$Tr4.105803@attbi_s03>,
    says...
    > > The problem is that the tsc data is not synched between CPUs, meaning that
    > > your results are only accurate as long as your process (or thread) is
    > > pinned to a specific CPU. That's usually not the case (nor ideal).

    >
    > I have a post to comp.sys.intel to see if anyone there knows. I know TSC
    > is zero at reset, and if all leave reset at the same time, they may be
    > synchronized. I will see if anyone answers there.


    Been there done that. The folks I asked at Intel admit that is the case.
    It's probably documented as such formally. I believe it was discussed
    recently on the threading forum at Intel's website and verified as
    unreliable" by Intel employees there. It has certainly been discussed in
    plenty of places. Please let me know if you get a different answer, but I
    have seen it give erratic results (I.e. negative tsc differences inside of
    wraparound times) due to TSC skew between processors. You may need to
    have a lot of dynamic load on the box such that the scheduler is moving
    processes around between CPUs before it becomes obvious.

    --
    Randy Howard _o
    2reply remove FOOBAR \<,
    ______________________()/ ()______________________________________________
    SCO Spam-magnet:
    Randy Howard, Oct 29, 2003
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    702
    Kelsang Wangchuk
    Jul 31, 2003
  2. Replies:
    1
    Views:
    1,572
    Steve C. Orr [MVP, MCSD]
    Feb 22, 2005
  3. Simon

    page timer, or redirect timer

    Simon, Nov 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    11,180
    Bruce Barker
    Nov 4, 2005
  4. gregg

    timer in milliseconds

    gregg, Dec 19, 2003, in forum: C Programming
    Replies:
    9
    Views:
    1,636
    Michel Bardiaux
    Dec 22, 2003
  5. Replies:
    8
    Views:
    476
    Jorgen Grahn
    Jul 15, 2013
Loading...

Share This Page