Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Dec 16, 2015



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Find Compilation Time of any Code and Execution Time of any Command

This is C source code written in Gcc platform (Linux, Ubuntu) that finds the compilation time of any other source code (e.g. C, C++, Java & others). It also finds the execution time of any command of Ubuntu in three time units: nanosecond, microsecond and millisecond.

Note: If you want to find the compilation time of a piece of Java source code using this C source code, the Java compiler must be installed in the machine.

#include <sys/time.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

int main()
   struct timespec ts1;
   struct timespec ts2;
   char str[20];

   printf("\n\tCompile the code/ Execute the Commamnd: ");
   fgets(str, sizeof str, stdin);

   clock_gettime(CLOCK_REALTIME, &ts1);
   system (str);
   clock_gettime(CLOCK_REALTIME, &ts2);

   if (ts2.tv_nsec     { 
      ts2.tv_nsec= ts2.tv_nsec+ pow(10,9);      
      printf("\n\tCompilation time:\n\tNanosecond: %ld\n\tMicrosecond: %ld\n\tMillisecond: %ld\n", (ts2.tv_nsec - ts1.tv_nsec),  (ts2.tv_nsec - ts1.tv_nsec)/1000, (ts2.tv_nsec - ts1.tv_nsec)/1000000);
      printf("\n\tCompilation time:\n\tNanosecond: %ld\n\tMicrosecond: %ld\n\tMillisecond: %ld\n", (ts2.tv_nsec - ts1.tv_nsec),  (ts2.tv_nsec - ts1.tv_nsec)/1000, (ts2.tv_nsec - ts1.tv_nsec)/1000000);

   return 0;
Arup Goswami
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date