Skip to content

Instantly share code, notes, and snippets.

@sumanth232
Created September 29, 2013 08:35
Show Gist options
  • Save sumanth232/04e2deaaaff4fe45c64f to your computer and use it in GitHub Desktop.
Save sumanth232/04e2deaaaff4fe45c64f to your computer and use it in GitHub Desktop.
Measure the time of a function in seconds
#include<stdio.h>
#include<conio.h>
#include<time.h>
void fun()
{
printf("fun() starts \n");
printf("Press enter to stop fun \n");
while(1)
{
if (getchar())
break;
}
printf("fun() ends \n");
}
int main()
{
// Calculate the time taken by fun()
clock_t t;
t = clock();
fun();
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("fun() took %f seconds to execute \n", time_taken);
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment