devxlogo

Find Compilation Time (in nanosecond, microsecond & millisecond) of any code (C, C++, JAVA & all others) & Execution Time of any Command

Find Compilation Time (in nanosecond, microsecond & millisecond) of any code (C, C++, JAVA & all others) & Execution Time of any Command

This is a C source code, written in gcc platform (Ubuntu, Linux). It finds the Compilation time of any Source code (like C, C++, Java & all others) as well as the Execution time of any command of Ubuntu in three time units, i.e nanosecond, microsecond & millisecond. 

Note: Let I want to find compilation time of a Java source code using this C source code. Hence Java compiler must be installed in the machine. 
The following C source code:

#include 
#include 
#include 
#include 

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

   printf(”
Compile 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(”
Compilation time:
Nanosecond: %ld
Microsecond: %ld
Millisecond: %ld
“, (ts2.tv_nsec – ts1.tv_nsec),  (ts2.tv_nsec – ts1.tv_nsec)/1000, (ts2.tv_nsec – ts1.tv_nsec)/1000000);
   }
   else
      printf(”
Compilation time:
Nanosecond: %ld
Microsecond: %ld
Millisecond: %ld
“, (ts2.tv_nsec – ts1.tv_nsec),  (ts2.tv_nsec – ts1.tv_nsec)/1000, (ts2.tv_nsec – ts1.tv_nsec)/1000000);

   return 0;
}
  

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist