-4

Is there any code in c to analyse the time taken by the program after giving the inputs so that we can find time complexity of program by giving different inputs. But the error of 1 millisecond can't be tolerated.i want exact time .

S.Mittal
  • 11
  • 3
  • 1
    See ["How do I measure time in C?"](https://stackoverflow.com/questions/3557221/how-do-i-measure-time-in-c) – Mor A. Sep 12 '17 at 14:09
  • How do you propose to use the elapsed time to determine the algorithmic complexity? – Jim Mischel Sep 12 '17 at 14:18
  • What is exact time? – ad absurdum Sep 12 '17 at 14:20
  • The term "time complexity" (google that) is misleading, you want to know how to get the "execution time" of your program. – Jabberwocky Sep 12 '17 at 14:26
  • 1
    You will never get the 'exact time' if you are running the program under an operating system such as Windows, Linux or macOS. The OS is always running other code as well as yours so the execution time will be different each time you run the program. – greg-449 Sep 12 '17 at 14:38
  • "Time complexity" and "exact time" do not go together very well... – Gerhardh Sep 12 '17 at 14:50
  • instead of insisting on sub-ms accuracy, run your program 1000 or 10,000 times to estimate its execution time – Frederick Sep 12 '17 at 15:04
  • You'll need to elaborate on what you're trying to do. If you're timing a process that takes more than a second, then one millisecond resolution is going to be more than sufficient. And if what you're doing takes less than a second, then you're decidedly *not* trying to determine what is normally considered "time complexity." – Jim Mischel Sep 12 '17 at 15:56

2 Answers2

2

there are functions to calculate time in time.h

just use it as the following example

clock_t start = clock();
 // your job
clock_t end = clock();
float seconds = (float)(end - start) / CLOCKS_PER_SEC;
Mehdi Ben Hamida
  • 893
  • 4
  • 16
  • 38
2

If you're timing the whole program, you don't even need to do any extra coding as there is the time command

> time ./a.out

which would give an output like

real    0m5.002s
user    0m0.001s
sys     0m0.001s
Chris Turner
  • 8,082
  • 1
  • 14
  • 18