Created
February 23, 2021 22:24
-
-
Save bishil06/0b2d4f73186baf827618094f619ffeed to your computer and use it in GitHub Desktop.
C show time complexity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
#include <math.h> | |
int timeOne(n) { | |
return n; | |
} | |
void timeN(n) { | |
while(n) { | |
n -= 1; | |
} | |
} | |
void timeLogN(logN) { | |
while(logN) { | |
logN -= 1; | |
} | |
} | |
void timeNLogN(n, logN) { | |
while(n) { | |
n -= 1; | |
while(logN) { | |
logN -= 1; | |
} | |
} | |
} | |
void timeNxN(n) { | |
int n1 = n; | |
while (n1) { | |
n1 -= 1; | |
int n2 = n; | |
while (n2) { | |
n2 -= 1; | |
} | |
} | |
} | |
void timeNxNxN(n) { | |
int n1 = n; | |
while (n1) { | |
n1 -= 1; | |
int n2 = n; | |
while (n2) { | |
n2 -= 1; | |
int n3 = n; | |
while (n3) { | |
n3 -= 1; | |
} | |
} | |
} | |
} | |
int fibo(n) { | |
if (n == 0 || n == 1) { | |
return 1; | |
} | |
return fibo(n-1) + fibo(n-2); | |
} | |
void timePow2N(n) { | |
fibo(n); | |
} | |
void timeNFactorial(n) { | |
if (n == 0) return; | |
int n1 = n; | |
while (n1) { | |
n1 -= 1; | |
timeNFactorial(n-1); | |
} | |
} | |
void printTimeComplexity(long n) { | |
clock_t start1; | |
clock_t end1; | |
printf("n = %ld\n", n); | |
if (n < 1000000000) { | |
start1 = clock(); | |
timeN(n); | |
end1 = clock(); | |
printf("N Time: %lfs\n",(double)(end1 - start1)/CLOCKS_PER_SEC); | |
} | |
start1 = clock(); | |
timeOne(n); | |
end1 = clock(); | |
printf("1 Time: %lfs\n",(double)(end1 - start1)/CLOCKS_PER_SEC); | |
start1 = clock(); | |
int logN = ceil(log(n)); | |
timeLogN(logN); | |
end1 = clock(); | |
printf("logN Time: %lfs\n",(double)(end1 - start1)/CLOCKS_PER_SEC); | |
if (n < 1000000000) { | |
int logN = ceil(log(n)); | |
start1 = clock(); | |
timeNLogN(n, logN); | |
end1 = clock(); | |
printf("NlogN Time: %lfs\n",(double)(end1 - start1)/CLOCKS_PER_SEC); | |
} | |
if (n<32000) { | |
start1 = clock(); | |
timeNxN(n); | |
end1 = clock(); | |
printf("N^2 Time: %lfs\n",(double)(end1 - start1)/CLOCKS_PER_SEC); | |
} | |
if (n<1000) { | |
start1 = clock(); | |
timeNxNxN(n); | |
end1 = clock(); | |
printf("N^3 Time: %lfs\n",(double)(end1 - start1)/CLOCKS_PER_SEC); | |
} | |
if (n < 43) { | |
start1 = clock(); | |
timePow2N(n); | |
end1 = clock(); | |
printf("2^N Time: %lfs\n",(double)(end1 - start1)/CLOCKS_PER_SEC); | |
} | |
if (n < 15) { | |
start1 = clock(); | |
timeNFactorial(n); | |
end1 = clock(); | |
printf("N! Time: %lfs\n",(double)(end1 - start1)/CLOCKS_PER_SEC); | |
} | |
} | |
int main(void) { | |
long n = 100000000000; | |
for (long i=1; i<=100000000000; i *= 10) { | |
printTimeComplexity(i); | |
printf("\n"); | |
} | |
return 0; | |
} | |
/* | |
n = 1 | |
N Time: 0.000004s | |
1 Time: 0.000000s | |
logN Time: 0.000015s | |
NlogN Time: 0.000000s | |
N^2 Time: 0.000001s | |
N^3 Time: 0.000000s | |
2^N Time: 0.000000s | |
N! Time: 0.000001s | |
n = 10 | |
N Time: 0.000001s | |
1 Time: 0.000000s | |
logN Time: 0.000001s | |
NlogN Time: 0.000002s | |
N^2 Time: 0.000001s | |
N^3 Time: 0.000003s | |
2^N Time: 0.000001s | |
N! Time: 0.046212s | |
n = 100 | |
N Time: 0.000001s | |
1 Time: 0.000000s | |
logN Time: 0.000001s | |
NlogN Time: 0.000001s | |
N^2 Time: 0.000038s | |
N^3 Time: 0.003768s | |
n = 1000 | |
N Time: 0.000004s | |
1 Time: 0.000000s | |
logN Time: 0.000001s | |
NlogN Time: 0.000004s | |
N^2 Time: 0.003347s | |
n = 10000 | |
N Time: 0.000035s | |
1 Time: 0.000000s | |
logN Time: 0.000000s | |
NlogN Time: 0.000035s | |
N^2 Time: 0.216611s | |
n = 100000 | |
N Time: 0.000213s | |
1 Time: 0.000000s | |
logN Time: 0.000000s | |
NlogN Time: 0.000217s | |
n = 1000000 | |
N Time: 0.002096s | |
1 Time: 0.000001s | |
logN Time: 0.000000s | |
NlogN Time: 0.002118s | |
n = 10000000 | |
N Time: 0.020733s | |
1 Time: 0.000001s | |
logN Time: 0.000001s | |
NlogN Time: 0.021077s | |
n = 100000000 | |
N Time: 0.204661s | |
1 Time: 0.000000s | |
logN Time: 0.000001s | |
NlogN Time: 0.209735s | |
n = 1000000000 | |
1 Time: 0.000000s | |
logN Time: 0.000001s | |
n = 10000000000 | |
1 Time: 0.000000s | |
logN Time: 0.000000s | |
n = 100000000000 | |
1 Time: 0.000000s | |
logN Time: 0.000002s | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment