how to compute time complexity

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...
What do you mean by time complexity? How long it takes to compute the code?
Why should you find the complexity of those programs?

http://en.wikipedia.org/wiki/Computational_complexity_theory
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.. :-(
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
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
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.