05 January 2012

How to calculate execution time of your C program

Here is a short C program that will calculate the time of 300 squares and 300 cube of i ( where i is 1,2,3........300 )

/* timer.c - simple example of timing a piece of code */ 
#include <stdio.h> 
#include <sys/types.h> 
#include <time.h> 
main() 

int i; 
time_t t1,t2; 
(void) time(&t1); 

for (i=1;i<=300;++i) 

 
     printf("%d %d %d\n",i, i*i, i*i*i);
   
   
   (void) time(&t2); 
 
   printf("\nTime to do 300 squares and cubes= %d seconds\n", (int) t2-t1); 
 
}
Here we have used function time(&t1) that will calculate the time before the execution 
of the loop and save its value at the address of t1 and another function time(&t2)
which saves time after the loop is executed. In printf statement we are printing the 
difference of two time which is the time in which the loop is executed . hence you can
use this technique to find out your program execution timing.
Note : don't forget to add library files before compiling your C program.

2 comments:


  1. This professional hacker is absolutely reliable and I strongly recommend him for any type of hack you require. I know this because I have hired him severally for various hacks and he has never disappointed me nor any of my friends who have hired him too, he can help you with any of the following hacks:

    -Phone hacks (remotely)
    -Credit repair
    -Bitcoin recovery (any cryptocurrency)
    -Make money from home (USA only)
    -Social media hacks
    -Website hacks
    -Erase criminal records (USA & Canada only)
    -Grade change

    Email: onlineghosthacker247@ gmail .com

    ReplyDelete
  2. Do you need to increase your credit score?
    Do you intend to upgrade your school grade?
    Do you want to hack your cheating spouse Email, whatsapp, Facebook, instagram or any social network?
    Do you need any information concerning any database.
    Do you need to retrieve deleted files?
    Do you need to clear your criminal records or DMV?
    Do you want to remove any site or link from any blog?
    you should contact this hacker, he is reliable and good at the hack jobs..
    contact : cybergoldenhacker at gmail dot com

    ReplyDelete