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,900 questions

51,831 answers

573 users

How to measure time in nanoseconds with C

1 Answer

0 votes
#include <stdio.h>
#include <time.h>
#include <stdint.h>

int main() {
    // Record start time
    struct timespec start, end;
    clock_gettime(CLOCK_MONOTONIC, &start);

    for (int i = 0; i < 1000; i++);

    // Record end time
    clock_gettime(CLOCK_MONOTONIC, &end);

    // Calculate time difference in nanoseconds
    int64_t time_in_nanoseconds = 
        (int64_t)(end.tv_sec - start.tv_sec) * 1000000000 + 
        (end.tv_nsec - start.tv_nsec);

    printf("%ld [ns]\n", time_in_nanoseconds);

    return 0;
}



/*
run:

751 [ns]

*/

 



answered Mar 14, 2025 by avibootz

Related questions

1 answer 187 views
187 views asked Feb 22, 2022 by avibootz
1 answer 241 views
2 answers 138 views
2 answers 140 views
140 views asked Jun 28, 2024 by avibootz
1 answer 123 views
123 views asked May 29, 2023 by avibootz
1 answer 227 views
...