1

I have simple function which takes random words and puts them in lexicographical order using insertion sort algorithm.I have no problem with function(It works,tested),but when i try to measure execution time of function using two different clock() values, i get same values before and after the execution of function,so it shows 0 as elapsed time

                clock_t t1 = clock();
                InsertionSort(data, n);
                clock_t t2 = clock();

                /*
                * Display the results.
                */

                for (size = i, i = 0; i < size; ++i)
                {
                    printf("data[%d] = \"%s\"\n", (int)i, data[i]);
                }

                /*
                * Display the execution time
                */
                printf("The time taken is.. %g ", (t2 -t1));
Wardruna
  • 198
  • 4
  • 23
  • Btw if i put clock t2 after "for loop",it shows difference so elapsed time – Wardruna May 13 '15 at 10:13
  • 1
    Possibly because of the granularity of `clock()`. In `time.h` in my MSVC there is `#define CLOCKS_PER_SEC 1000` – Weather Vane May 13 '15 at 10:18
  • Same here i use visual studio 2013.CLOCKS_PER_SEC defined as 1000.My problem is that "why t2 and t1 are the same"? – Wardruna May 13 '15 at 10:44
  • Haven't you answered your own question in your very first comment? The time difference is too small to be measured by this method, without adding more code to execute. – Weather Vane May 13 '15 at 11:04
  • @WeatherVane yes i thought the same thing, but i am a beginner C programmer so i was not sure of it.So is there a way you know to measure the execution time of this function? – Wardruna May 13 '15 at 11:11
  • 1
    Usually, you contrive a way to measure a large number of loops of what you want to time. 10, 100, 1000, whatever produces a significant result. Bear in mind too that on a multi-tasking OS each iteration will take a slightly different time, and so you'll also establish a typical average.The result might also be affected by processor caching and/or file caching. – Weather Vane May 13 '15 at 11:15
  • time taken to execute a complete function http://stackoverflow.com/a/40380118/6180077 – Abdullah Farweez May 17 '17 at 05:04

3 Answers3

0

Try like this:

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

double gettime(void)
{
   struct timediff td;
   double d=0;
   gettimeofday(&td, NULL);
   d=td.td_usec;
   d+= (double)td.td_usecs / 1000000.;
   return d;
}

double t1=gettime();
InsertionSort(data, n);
printf("%.6f", gettime() - t1);

or may be you need to change your code like this:

 clock_t t1 = clock();
 InsertionSort(data, n);
 clock_t t2 = clock();
 double d= double(t2- t1) / CLOCKS_PER_SEC;

You can also refer: Easily measure elapsed time

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Your solutions gave lots of error,i am trying to fix them.And as a reminder : This is a C program not C++. – Wardruna May 13 '15 at 10:47
  • @BerkKarabacak:- What is the error which you are getting? – Rahul Tripathi May 13 '15 at 10:51
  • `double(t2- t1)` --> `(double)(t2- t1)` – Weather Vane May 13 '15 at 10:57
  • For your second solution : when casting(t2-t1) to double it says "Expected an expression" For your first solution : Cannot open include file: 'sys/time.h': No such file or directory.And if i delete "sys" part from then it says 'td' uses undefined struct 'timediff" – Wardruna May 13 '15 at 11:00
  • @BerkKarabacak:- Can you try to use `double d= (double)(t2- t1) / CLOCKS_PER_SEC;`? – Rahul Tripathi May 13 '15 at 11:02
  • I have tried that,still zero.Why you hope getting different result?As long as t2 and t1 are same,it is gonna show zero – Wardruna May 13 '15 at 11:05
  • @BerkKarabacak: I did not hope that t2 and t1 to be same, I thought it would be a very small very value but certainly not 0. But looks like my assumption is wrong – Rahul Tripathi May 13 '15 at 11:06
  • I feel like before it finishes function,it runs "clock_t t2 = clock()" line but i guess it is not possible in C – Wardruna May 13 '15 at 11:07
  • @BerkKarabacak:- No thats not possible. clock_t t2 = clock() will be executed only after the method call – Rahul Tripathi May 13 '15 at 11:10
0

You are incorrectly using the floating-point format specifier %g. Try this

printf("The time taken is.. %u clock ticks", (unsigned)(t2 -t1));

Always assuming the execution time is longer than the granularity of clock().

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

The time difference is too small to be measured by this method, without adding more code to execute. – Weather Vane

Usually, you contrive a way to measure a large number of loops of what you want to time. 10, 100, 1000, whatever produces a significant result. Bear in mind too that on a multi-tasking OS each iteration will take a slightly different time, and so you'll also establish a typical average.The result might also be affected by processor caching and/or file caching. – Weather Vane

Armali
  • 18,255
  • 14
  • 57
  • 171