Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,883 questions

51,809 answers

573 users

How to use struct timeval to get the execution time in microseconds with C

1 Answer

0 votes
#include <stdio.h>
#include <sys/time.h>

/*
  struct timeval {
    long tv_sec;    // seconds
    long tv_usec;   // microseconds
};
*/

int main() {
    struct timeval timevalBefore, timevalAfter;  

    gettimeofday(&timevalBefore, NULL);
    
    for (int i = 0; i < 10000000; i++) ;

    gettimeofday(&timevalAfter, NULL);

    printf("%ld microseconds\n", timevalAfter.tv_usec - timevalBefore.tv_usec); 

    return 0;
}



/*
run:

19592 microseconds

*/

 



answered Mar 11, 2022 by avibootz

Related questions

1 answer 94 views
1 answer 238 views
2 answers 137 views
1 answer 122 views
122 views asked May 29, 2023 by avibootz
1 answer 135 views
135 views asked Sep 26, 2021 by avibootz
...