maxpesa
maxpesa

Reputation: 185

time(*time_t) does not get current time (homework)

we are making some reports about different sorting algorithms. They're all working, but I get an error when printing the processing time for merge sort

I wrote the following code

...BubbleSort(arr3,50000);

time(&time_now); 
f = difftime(time_now,time_then);
printf("BubbleSort - tempo: %f\n",f);

time(&time_then); 
printf("then: %s",ctime(&time_then));
MergeSort(arr4,0,49999);

time(&time_now); 
printf("now: %s",ctime(&time_now)); 
f = difftime(time_now,time_then);

printf("MergeSort - tempo: %f\n",f);

but it always says time = 0s for merge sort

enter image description here

it seems it can't get the current time or mergesort's processing time is really low (but it works) thanks in advance

Upvotes: 1

Views: 291

Answers (4)

Roy Longbottom
Roy Longbottom

Reputation: 1192

Depends if you want elapsed or CPU time. Following is for both.

  // On Raspberry Pi gcc timing.c -lrt -O3 -o timer

  #include <time.h>
  #include <stdio.h>

  double  theseSecs = 0.0;
  double  startSecs = 0.0;
  double  secs;
  double  CPUsecs = 0.0;
  double  CPUutilisation = 0.0;
  double  answer = 0;
  clock_t starts;

  void start_CPU_time()
  {      
      starts = clock();;
      return;
  }

  void end_CPU_time()
  {
      CPUsecs = (double)(clock() - starts)/(double)CLOCKS_PER_SEC;
      return;
  }    

  struct timespec tp1;
  void getSecs()
  {
     clock_gettime(CLOCK_REALTIME, &tp1);
     theseSecs =  tp1.tv_sec + tp1.tv_nsec / 1e9;           
     return;
  }

  void start_time()
  {
      getSecs();
      startSecs = theseSecs;
      return;
  }

  void end_time()
  {
      getSecs();
      secs    = theseSecs - startSecs;
      return;
  }    

  void calculate()
  {
      int i, j;
      for (i=1; i<100001; i++)
      {
          for (j=1; j<10001; j++)
          {
              answer = answer + (float)i / 100000000.0;
          }
      }
  }

 void main()
 {
     start_time();
     start_CPU_time();
     calculate();
     end_time();
     end_CPU_time();
     CPUutilisation = CPUsecs /  secs * 100.0;
     printf("\n Answer %10.1f, Elapsed Time %7.4f, CPU Time %7.4f, CPU Ut %3.0f%\n",
              answer, secs, CPUsecs, CPUutilisation);  
 }

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

You see zeros because your MergeSort is so fast that it completes in under a measurable time interval on your system.

Giving your program substantially more data to sort should help. Alternatively, you could change the time measurement mechanism for something more precise. The second approach is system-dependent, though.

The execution time for MergeSort grows as N*log2(N), so to see a time comparable with that of BubbleSort which grows as N2, you need to give it significantly more data. For an array of 50,000 items the math works out to approximately 3,000 times the size. If you pass MergeSort an array of roughly 50,000*3,000=150,000,000 items, you should see a non-zero number printed.

Note: Do not try passing that much data to BubbleSort - it would take it ages to complete, unless the data is already pretty close to being sorted.

Upvotes: 7

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

time function and time_t type granularity is usually seconds, so if your function returns in less than 1 second the diff will be zero. For quick and dirty benchmarking you can use clock().

Upvotes: 0

Sergey L.
Sergey L.

Reputation: 22542

Do not use time to time the runtime of programs/algorithms. This is the global system time and includes preemption time and other programs running the background.

Use getrusage to get the resource (CPU time) usage of your code.

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



struct rusage start, end;
getrusage(RUSAGE_SELF, &start);

// run your code

getrusage(RUSAGE_SELF, &end);

struct timeval used_utime, used_stime;
timersub(&end.ru_utime, &start.ru_utime, &used_utime);
timersub(&end.ru_stime, &start.ru_stime, &used_stime);
printf("function ran for %d usec in user mode and %d usec in system mode \n"
    , used_utime.tv_sec * 1000 * 1000 + used_utime.tv_usec
    , used_stime.tv_sec * 1000 * 1000 + used_stime.tv_usec);

Upvotes: 6

Related Questions