waittime function precision

Jevgeni Astanovski <[email protected]>
Newsgroups gmane.comp.lang.as400.c
Message-ID <[email protected]>
Hi,
I've used waittime function for pretty long time in cases, where I needed to have a delay in my program.
But recently a question appeared - how precise it is and are there any alternatives that allow to make delays for less than 1/100 of a second.
For an experiment I wrote a short program:

int main (int argc, char *argv[])                                    
{                                                                    
  _MI_Time WaitTime ;                                                
  struct timeval tp1, tp2 ;                                          
  struct timezone tpz ;                                              
  decimal (9,3) Duration ;                                           
  decimal (19,6) StartTime, EndTime ;                                
  int i ;                                                            
                                                                     
  for (i = 1; i < 10; i++)                                           
  {                                                                  
    gettimeofday(&tp1, &tpz) ;                                       
    StartTime = tp1.tv_usec ;                                        
    StartTime = tp1.tv_sec + (StartTime / 1000000D) ;                
                                                                     
    mitime(&WaitTime, 0, 0, 0, i) ;  /*hours,minutes,seconds,1/100*/ 
    waittime(&WaitTime, _WAIT_NORMAL) ;                              

    gettimeofday(&tp2, &tpz) ;                                         
    EndTime = tp2.tv_usec ;                                            
    EndTime = tp2.tv_sec + (EndTime / 1000000D) ;                      
    Duration = (decimal (9,3)) ((EndTime - StartTime) * 1000D) ;       
    printf("Delay = %d ms, waittime: %D(9,3) ms\n", i * 10, Duration) ;
  }                                                                    
  return(0);                                                           
}                                                                      

So it makes 10 cycles producing delays (theoretically) 10, 20, 30... 100 ms.

And here's the result:
Delay = 10 ms, waittime: 18.480 ms  
Delay = 20 ms, waittime: 38.960 ms  
Delay = 30 ms, waittime: 59.440 ms  
Delay = 40 ms, waittime: 69.984 ms  
Delay = 50 ms, waittime: 50.552 ms  
Delay = 60 ms, waittime: 84.936 ms  
Delay = 70 ms, waittime: 83.336 ms  
Delay = 80 ms, waittime: 109.912 ms 
Delay = 90 ms, waittime: 119.136 ms

Isn't it strange?
I have no reasons to think that gettimeofday gives invalid resuts and the calculations are pretty simple...
Is it a fundamental feature of waittime?

Jevgeni

-- 
This is the Bare Metal Programming IBM i (AS/400 and iSeries) (C400-L) mailing list
To post a message email: C400-L-Zwy7GipZuJhWk0Htik3J/[email protected]
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/c400-l
or email: C400-L-request-Zwy7GipZuJhWk0Htik3J/[email protected]
Before posting, please take a moment to review the archives
at http://archive.midrange.com/c400-l.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.