Advertisement
dmilicev

timer_function_without_time.h_v1.c

Nov 20th, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. /*
  2.  
  3.     timer_function_without_time.h_v1.c      by Dragan Milicev
  4.  
  5.     Idea is to make timer function without the use of timer.h header file.
  6.  
  7.     In function
  8.     void my_sleep( int milliseconds )
  9.     I use 3 nested for loops i, j and k,
  10.     to make the required time delay during the run.
  11.  
  12.     But, required time delay depends on the speed of your machine's processor.
  13.  
  14.     So I temporarily use header file time.h to set my function to the speed of my processor.
  15.     You do the same.
  16.  
  17.     Setup:
  18.  
  19.     Important information for your computer processor is CLOCKS_PER_SEC.
  20.     The program will print to you its value, which you will write
  21.     instead of CLOCKS_PER_SEC in function my_sleep(milliseconds);
  22.  
  23.     main() is set to have a time delay of 10 seconds.
  24.  
  25.     Change the #define SETUP_NUMBER 400
  26.     until you get an approximate time of 10,000 milliseconds.
  27.  
  28.     Write the resulting value of SETUP_NUMBER
  29.     into the function my_sleep(milliseconds);
  30.  
  31.     After this setup, you will get the my_sleep(milliseconds);
  32.     function which will work accurately enough on your computer
  33.     without heder file time.h .
  34.  
  35.  
  36.     You can find all my C programs at Dragan Milicev's pastebin:
  37.  
  38.     https://pastebin.com/u/dmilicev
  39.  
  40.     https://www.facebook.com/dmilicev
  41.  
  42. */
  43.  
  44. #include <stdio.h>
  45. #include <time.h>
  46.  
  47. #define SETUP_NUMBER 400
  48.  
  49. void my_sleep( int milliseconds )
  50. {
  51.     int i, j, k;
  52.  
  53.     for(i=0; i<milliseconds; i++)
  54.         for(j=0; j<SETUP_NUMBER; j++)
  55.             for(k=0; k<CLOCKS_PER_SEC; k++);
  56. }
  57.  
  58. int main () {
  59.  
  60.     clock_t start_t, end_t, total_t;
  61.     int milliseconds = 10000;   // main() is set to have a time delay of 10 seconds.
  62.  
  63.     start_t = clock();
  64.  
  65.     printf("\n Starting of the my_sleep(%d) function, start_t = %ld \n", milliseconds, start_t );
  66.  
  67.     printf("\n Wait 10 seconds ... \n");
  68.  
  69.     my_sleep(milliseconds);     // to delay (wait, sleep) 10 seconds
  70.  
  71.     end_t = clock();
  72.  
  73.     printf("\n End of the my_sleep(%d) function, end_t = %ld \n", milliseconds, end_t );
  74.  
  75.     total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
  76.  
  77.     printf("\n Total time taken by CPU: %ld \n", total_t  );
  78.  
  79.     printf("\n start_t = %ld \t end_t = %ld \t total_t = %ld \n", start_t, end_t, total_t  );
  80.  
  81.     printf("\n CLOCKS_PER_SEC = %5ld \n", CLOCKS_PER_SEC );
  82.  
  83.     printf("\n SETUP_NUMBER   = %5d \n", SETUP_NUMBER );
  84.  
  85.  
  86.     return(0);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement