NO

Author Topic: milliseconds  (Read 5212 times)

goldenfiretech

  • Guest
milliseconds
« on: February 20, 2012, 04:26:45 AM »
Hi

I have been trying very hard, but I can't seem to get Pelles C to give me the time in milliseconds, in a for loop.  I need to have the program print the time in milliseconds.  The included code will work and print the milliseconds, but they are all the same number or all but one is a different number.  How can I get this program to give me a broader range?  I want to use the milliseconds as a seed for srand.  But I need to make the seeds very different.

Thanks,
Mike

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

   int main ()
   {
   int t;
   struct _timeb now;
 
       _ftime(&now);
 
   for ( t = 0; t < 10; t+=1 )
   {
          _ftime(&now);
    printf (" time = < %12u >", now.time );
    printf (" milliseconds = < %6d >\n", now.millitm );

   }

      return 0;
   }

CommonTater

  • Guest
Re: milliseconds
« Reply #1 on: February 20, 2012, 04:48:51 AM »
For a reasonably accurate millisconds count, try the windows API functions...

GetTickCount()

GetLocalTime() 
 
GetSystemTime() 
 
Etc...
 
I  note that your for loop only goes to 10 ... most likely it's done in microseconds, certainly less than a millisecond on any modern machine...
 
Also the best way to seed srand() is like this...
Code: [Select]
#include <time.h>
#include <stdlib.h>
 
int main (void)
  {
      srand(time(NULL));  // call this exactly once
 
      // rest of program
 
      x = rand() % limit;  // call this as often as you need
 
      // more code
      return 0 ;
  }
 

You only use srand() once in your program; to create a cache of random number. By using time() you will get a different sequence everytime (unless you call it twice in the same second). You can then call rand() as often as you like... 
 
« Last Edit: February 20, 2012, 04:58:37 AM by CommonTater »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: milliseconds
« Reply #2 on: February 20, 2012, 05:41:13 AM »
For a reasonably accurate millisconds count, try the windows API functions...

GetTickCount()
That's probably not going to do much good, depending on what the OP's intentions are...
Quote from: MSDN
Remarks

The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds. The resolution of the GetTickCount function is not affected by adjustments made by the GetSystemTimeAdjustment function.
Quote
GetLocalTime() 
 
GetSystemTime() 
 
Etc...
 
I  note that your for loop only goes to 10 ... most likely it's done in microseconds, certainly less than a millisecond on any modern machine...
Not only that, timing in a single thread/program suffers inherently from inaccuracy due to the multitasking nature of Windows...

None of the standard API calls will help here, you need to have some routines that get a more accurate counter from the CPU itself. One such function can be found at

http://cplus.about.com/od/howtodothingsin1/a/timing.htm

Ralf

CommonTater

  • Guest
Re: milliseconds
« Reply #3 on: February 20, 2012, 02:35:48 PM »
Hi Ralf ...

Thanks for chiming in.  I'm aware of the innacuracies and would not suggest the API functions where a high precision timer is needed... Heck I wouldn't even suggest Windows in that case :D   

However; the OP has said that he's just wanting it for the srand() function where any non-repeating number will do.  I've used GetTickCount() and time(NULL) in that case before and either should be adequate.
« Last Edit: February 20, 2012, 02:41:26 PM by CommonTater »

goldenfiretech

  • Guest
Re: milliseconds
« Reply #4 on: February 20, 2012, 11:46:12 PM »
Hi!

Thank you for your help!  I modified CommonTater's code and it worked wonderfully for me!  Thank you everyone for your help!

Mike

CommonTater

  • Guest
Re: milliseconds
« Reply #5 on: February 21, 2012, 01:37:52 AM »
Thank you for your help!  I modified CommonTater's code and it worked wonderfully for me!  Thank you everyone for your help!

No worries... glad to help out.
There's a pretty good crew on this forum, lots of really good programmers.

czerny

  • Guest
Re: milliseconds
« Reply #6 on: May 09, 2012, 11:24:25 AM »
None of the standard API calls will help here, you need to have some routines that get a more accurate counter from the CPU itself. One such function can be found at

http://cplus.about.com/od/howtodothingsin1/a/timing.htm

Ralf
Here is a PellesC project (time in milliseconds not seconds)