🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The clock() function in C is a standard library function that returns the processor time consumed by the program. It is part of the C standard library (time.h). This function is useful for measuring the performance of a program or specific parts of a program.
Table of Contents
- Introduction
clock()Function Syntax- Examples
- Measuring Execution Time of a Code Block
- Using
clock()with User Input
- Real-World Use Case
- Conclusion
Introduction
The clock() function returns the number of clock ticks elapsed since the program was launched. This can be used to calculate the processor time consumed by the program. The constant CLOCKS_PER_SEC can be used to convert clock ticks to seconds.
clock() Function Syntax
The syntax for the clock() function is as follows:
#include <time.h>
clock_t clock(void);
Returns:
- The function returns the number of clock ticks since the program started. If the processor time is not available or its value cannot be represented, the function returns
-1.
Examples
Measuring Execution Time of a Code Block
To demonstrate how to use clock() to measure the execution time of a code block, we will write a simple program that calculates the time taken to execute a loop.
Example
#include <stdio.h>
#include <time.h>
int main() {
clock_t start, end;
double cpu_time_used;
int i;
// Start the clock
start = clock();
// Code block to measure
for (i = 0; i < 1000000; i++);
// End the clock
end = clock();
// Calculate the time taken
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
// Print the result
printf("Time taken to execute the loop: %f seconds\n", cpu_time_used);
return 0;
}
Output:
Time taken to execute the loop: 0.010000 seconds
Using clock() with User Input
This example shows how to use clock() to measure the time taken by the user to enter a string.
Example
#include <stdio.h>
#include <time.h>
int main() {
clock_t start, end;
double cpu_time_used;
char input[100];
// Start the clock
start = clock();
// Get user input
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
// End the clock
end = clock();
// Calculate the time taken
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
// Print the result
printf("Time taken to enter the string: %f seconds\n", cpu_time_used);
return 0;
}
Output (example user input time of 3 seconds):
Enter a string: Hello World
Time taken to enter the string: 3.000000 seconds
Real-World Use Case
Profiling Program Performance
In real-world applications, the clock() function can be used to profile the performance of a program, identifying bottlenecks and optimizing code execution.
Example: Profiling a Function
#include <stdio.h>
#include <time.h>
// Function to profile
void perform_task() {
for (int i = 0; i < 1000000; i++);
}
int main() {
clock_t start, end;
double cpu_time_used;
// Start the clock
start = clock();
// Call the function to profile
perform_task();
// End the clock
end = clock();
// Calculate the time taken
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
// Print the result
printf("Time taken to execute perform_task(): %f seconds\n", cpu_time_used);
return 0;
}
Output:
Time taken to execute perform_task(): 0.010000 seconds
Conclusion
The clock() function is essential for measuring the processor time consumed by a program in C. It is useful in various applications, particularly in performance profiling and optimization, where it is necessary to measure and improve the execution time of code blocks or functions.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment