ADVERTISEMENT
ADVERTISEMENT

C program to calculate total elapsed time by program/function

In this program, we are going to learn how to calculate the total elapsed time by a function/program i.e., the time taken to process the set of statements, complete function or complete program in C programming language?

Program:

// C program to calculate total elapsed time
// by program/ function.

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

int main()
{
    time_t prg_begin, prg_end;
    char str[100];

    prg_begin = clock();
    
    printf("Enter any string :");
    gets(str);
    printf("Entered value is :%s\n", str);
    
    prg_end = clock();
    
    printf("%f seconds taken by this program...", (double)(prg_end - prg_begin) / (double)CLK_TCK);
    
    return 0;
}

Output:

Enter any string :www.includehelp.com
Entered value is :www.includehelp.com
4.865000 seconds taken by this program...

Explanation:

  • clock() : Returns number of clock ticks since program start.
  • clock_t : Type returned by clock() function.
  • CLK_TCK : Number of ticks in a second.

Note: To determine the time in seconds, divide the value returned by clock by the value of the macro CLK_TCK. You can also use CLOCKS_PER_SEC instead of CLK_TCK.

C Miscellaneous Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.