how to compute time complexity
Aug 11, 2009 at 5:15pm UTC
Hello everyone!
Can someone help me out how to compute the time complexity of this codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <stdio.h>
void main()
{
int i;
for (i = -20; i <=-11; i++)
printf("%d " , i);
printf("\n" );
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
void main()
{
int i;
for (i = 0; i <= 10; i++)
printf("%d " , i*i);
printf("\n" );
}
and if you know some sites that has codes and it's pseudocode in c++, kindly post it pls 'coz I need sample codes, Thank you in advance...
Aug 11, 2009 at 11:07pm UTC
What do you mean by time complexity? How long it takes to compute the code?
Aug 11, 2009 at 11:12pm UTC
Aug 12, 2009 at 7:04am UTC
I need to find the time complexity of those programs because this is a project given to me, and I totally don't know how to solve it.. :-(
Aug 12, 2009 at 8:02am UTC
so you don't need computational complexity for your codes? (IMO its O(10) in 1st case and O(11) in the second one, correct me if I'm wrong)
i guess you need execution time of both codes?
edit
If yes, you can use clock() function from <ctime> or GetTickCount() from <windows.h>
edit2
I even found a nice example:
http://www.dreamincode.net/forums/showtopic24685.htm
Last edited on Aug 12, 2009 at 8:20am UTC
Aug 12, 2009 at 8:23am UTC
From Wikipedia:
The time complexity of a problem is the number of steps that it takes to solve an instance of the problem as a function of the size of the input , using the most efficient known algorithm
Here is no input, so complexity = constant
Aug 12, 2009 at 11:21am UTC
Further to johnkravetzki's mention of the clock() function; here's an example:
1 2 3 4 5
double start = clock() // It has the "float" datatype
SomeFunction();
double end = clock(); // Take the time again.
double duration = end - start / CLOCKS_PER_TICK;
Topic archived. No new replies allowed.