How to measure function execution time in C

4 Answers

0 votes
#include <stdio.h>
#include <time.h>
 
// Use clock() - Measures CPU time
 
void my_function() {
    volatile long long sum = 0;
    for (long long i = 0; i < 100000000; ++i) {
        sum += i;
    }
}
 
int main() {
    clock_t start = clock();
 
    my_function();
 
    clock_t end = clock();
 
    double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
    printf("Time: %f seconds\n", elapsed);
}
 
  
/*
run:
 
Time: 0.088708 seconds
  
*/

 



answered May 27 by avibootz
edited May 27 by avibootz
0 votes
#include <stdio.h> 
#include <time.h> 
  
// Use clock_gettime() - Nanosecond - Measures real elapsed time
  
void my_function() {
    volatile long long sum = 0;
    for (long long i = 0; i < 100000000; ++i) {
        sum += i;
    }
}
  
int main() {
    struct timespec start, end;
  
    clock_gettime(CLOCK_MONOTONIC, &start);
  
    my_function();
  
    clock_gettime(CLOCK_MONOTONIC, &end);
  
    double elapsed = (end.tv_sec - start.tv_sec)
                   + (end.tv_nsec - start.tv_nsec) / 1e9;
  
    printf("Time: %f seconds\n", elapsed);
}
  
   
   
/*
run:
   
Time: 0.090654 seconds
   
*/

 



answered May 27 by avibootz
edited May 27 by avibootz
0 votes
#include <stdio.h> 
#include <sys/time.h>
 
// Use wall‑clock timing: gettimeofday()
 
void my_function() {
    volatile long long sum = 0;
    for (long long i = 0; i < 100000000; ++i) {
        sum += i;
    }
}
 
int main() {
    struct timeval start, end;
 
    gettimeofday(&start, NULL);
 
    my_function();
 
    gettimeofday(&end, NULL);
 
    double elapsed = (end.tv_sec - start.tv_sec)
                   + (end.tv_usec - start.tv_usec) / 1e6;
 
    printf("Time: %f seconds\n", elapsed);
}
 
  
  
/*
run:
  
Time: 0.091170 seconds
  
*/

 



answered May 27 by avibootz
edited May 27 by avibootz
0 votes
// Measuring function execution time in C using a reusable Timer (milliseconds + seconds)

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

// ---------------------------
// Reusable Timer definition
// ---------------------------
typedef struct {
    clock_t start;
    clock_t end;
} Timer;

// Start the timer
void TimerStart(Timer* t) {
    t->start = clock();
}

// Stop the timer
void TimerStop(Timer* t) {
    t->end = clock();
}

// Return elapsed time in milliseconds
double TimerElapsedMilliseconds(const Timer* t) {
    return (double)(t->end - t->start) * 1000.0 / CLOCKS_PER_SEC;
}

// Return elapsed time in seconds
double TimerElapsedSeconds(const Timer* t) {
    return (double)(t->end - t->start) / CLOCKS_PER_SEC;
}

// ---------------------------
// Function to measure
// ---------------------------
void work() {
    volatile long long sum = 0;
    for (long long i = 0; i < 100000000; ++i)
        sum += i;
}

// ---------------------------
// Main program
// ---------------------------
int main() {
    Timer t;

    TimerStart(&t);
    work();
    TimerStop(&t);

    double ms = TimerElapsedMilliseconds(&t);
    double sec = TimerElapsedSeconds(&t);

    printf("Execution time: %.3f ms\n", ms);
    printf("Execution time: %.6f seconds\n", sec);

    return 0;
}



/* 
run:

Execution time: 89.871 ms
Execution time: 0.089871 seconds

*/

 



answered May 27 by avibootz
edited May 27 by avibootz
...