C Program to Time Command using clock_gettime

in #programming3 years ago

In C/C++, we can use the clock_gettime method from time.h to get the Seconds and Nano Seconds. The following C runs the external command and measure the time difference:

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
 
int main(int argc, char* argv[]) {
    char buf[1024];
    int j = 0;
    for (int i = 1; i < argc; ++ i) {
        size_t l = strlen(argv[i]);
        strcpy(buf + j, argv[i]);
        j += l;
        buf[j ++] = ' ';    
    }
    buf[j] = '\0';
    struct timespec curTime, nowTime;
    clock_gettime(CLOCK_REALTIME, &curTime);
    system(buf);
    clock_gettime(CLOCK_REALTIME, &nowTime);
    long int secs = nowTime.tv_sec - curTime.tv_sec;
    long int nsec = nowTime.tv_nsec - curTime.tv_nsec;
    printf("s = %ld\nns = %ld\n", secs, nsec);
    return 0;
}

Compiled with gcc:

$ gcc run.c -o run

And then, we can measure the time of a program (performance profiler):

$ ./run echo Hello, World!
Hello, World!
s = 0
ns = 1379618

--EOF (The Ultimate Computing & Technology Blog) --

Reposted to Blog

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^

NEW! Following my Trail (Upvote or/and Downvote)

Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com

My contributions

Delegation Service

Important Update of Delegation Service!

  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
    Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses

Sort:  

Well..!You have done "GOAT" on behalf of steemit media

Pls vigit my diary if you get time .

@justyy #affable #india

@justyy sir if you don't mind i have a asking..

Sir i have seen some user get 3.44$+ vote for 5000 delegation per day but i got 1.48$ highest for delegation 5050sp i am not clear here?

Can you clear me sir?

Actually i just invested after seen 3.44$+ for 5000 sp
i also invest more sp if i get 3.44$+. Now my target is 25k sp delegation but if i got 1.48 then nothing to say.

plz sir hope you clear me about it,thanks

Screenshot_20210722-001746_Chrome.jpg

Hi @justyy i have done

Screenshot_20210722_201107.jpg

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.028
BTC 74715.49
ETH 2808.16
USDT 1.00
SBD 2.53