Kay
Kay

Reputation: 845

Timing the runtime of a program via the Terminal

I've written a C++ program of which i would like to time the length of time it takes to complete - is there some terminal command i could use?

Upvotes: 3

Views: 3045

Answers (5)

Kohn1001
Kohn1001

Reputation: 3911

I try to better explain :)

If you have compiled your code using g++, for example:

g++ -std=c++14 c++/dijkstra_shortest_reach_2.cpp -o dsq

In order to run it, you type:

./dsq

In order to run it with a file content as an input, you type:

./dsq < input07Dijkstra.txt

Now for the answer.

In order to get the duration of the program output to the screen, just type:

time(./dsq < input07Dijkstra.txt)

Or without an input:

time(./dsq)

For the first command my output is:

real    0m16.082s
user    0m15.968s
sys     0m0.089s

Hope it helps!

Upvotes: 0

David Costa
David Costa

Reputation: 1818

You can use the "time" command available in most (maybe all) the linux distributions. It will print the time spent as system, as user, and the total time.

For example

bash-4.1$ time (sleep 1; sleep 1)

will output something like

real    0m2.020s
user    0m0.014s
sys 0m0.005s

As you can see with the parenthesis you can launch every command chain you wish.

Upvotes: 4

Eugen Constantin Dinca
Eugen Constantin Dinca

Reputation: 9150

Or Measure-Command in PowerShell.

Upvotes: 0

Jeff Lamb
Jeff Lamb

Reputation: 5865

Iterate over the function several times (1000's probably) so you can get a large enough number. Then use time.h to create two variables of type time_t - one before execution, one after. Subtract the two and divide by the iterations.

Upvotes: 0

Nim
Nim

Reputation: 33655

It's called time in *nix

Upvotes: 3

Related Questions