I have two programs which gives a same output.....But they uses different logics....
So I want to compare their compilation and run time so I can understand which one is better....
The program gives an output like.....If u enter a number=789 then output=890 means each input digit got increament of 1
(1)
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
...{
int no, no1, no2, counter, ans;
ans=0;
counter=0;
printf("Enter any three digit number:");
scanf("%d" , &no );
while(no>0)
{
no1=no%10;
if(no1==9)
{
no2=0;
}
else
{
no2=no1+1;
}
ans=ans+(no2*pow(10,counter));
no=no/10;
counter=counter+1;
}
printf("ans is %d" , ans);
getch();
return 0;
}
(2)
#include <stdio.h>

int main()
{
unsigned int num, out=0, weight=1;

printf("Enter a number: ");
scanf("%u", &num);

while ( num > 0 )
{
out = out + ((((num % 10) + 1) % 10) * weight);
num = num / 10;
weight = weight * 10;
}

printf("%u\n", out);

return 0;
}

I don't know why you need to know your compilation time but it's not that hard to perform. But in general as you compile once and you always in most cases running time is crucial to programmers not the compilation time.

As for running time that I think what you need, is indicated in the following piece of code:

clock_t start = clock();

// Here goes the process that you need
// to measure running time (could be your whole code)

cout << clock() - start << endl;

By the way, please put your code in code tags.

Edited 5 Years Ago by group256: n/a

This article has been dead for over six months. Start a new discussion instead.