Skip to content

Commit 270f292

Browse files
Merge pull request sanscript-tech#229 from Nibba2018/tim_prog_c
Checking Timing of a Program in C
2 parents e21c923 + c6bfea3 commit 270f292

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

C/Program-Timer/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Program Timing
2+
This script checks the time required to run a function in C.
3+
4+
5+
## To use
6+
* Edit `void function` and write your own operations.
7+
* Compile with `gcc timer.c -o timer`.
8+
* Execute with `./timer`
9+
10+
**Time Complexity:** O(1)
11+
**Space Complexity:** 0(1)
12+
13+
## Output
14+
![output](output.png)

C/Program-Timer/output.png

15.5 KB
Loading

C/Program-Timer/timer.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<time.h>
2+
#include<stdio.h>
3+
4+
// Placeholer function to edit:
5+
void function(){
6+
int a, b;
7+
printf("Enter numbers to add(space separated):");
8+
scanf("%d %d", &a, &b);
9+
printf("Sum of %d and %d is %d.\n", a, b, a+b);
10+
}
11+
12+
int main(int argc, char *argv[])
13+
{
14+
/*
15+
Create start and end clock variables of type `clock_t`
16+
and time the function.
17+
*/
18+
clock_t start = clock();
19+
function();
20+
clock_t end = clock();
21+
22+
// Divide clock difference with CLOCKS_PER_SEC to convert time to seconds
23+
// and then typecast it to double.
24+
double time_taken = ((double)(end - start))/CLOCKS_PER_SEC;
25+
26+
printf("function took %f seconds to execute\n", time_taken);
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)