Sarath Babu
Sarath Babu

Reputation: 922

How to compute run time for a C program in ubuntu

I tried >>time ./a.out

got

real 0m0.035s

user 0m0.008s

sys  0m0.004s

what it really means?

when I repeated I got

real 0m0.012s

user 0m0.000s

sys  0m0.012s

if I try this many times each time I am getting different output why? how can I solve it.

Upvotes: 2

Views: 13535

Answers (4)

Praveen Vinny
Praveen Vinny

Reputation: 2408

time -f "%e" -o Output.log ./a.out

will store the value of the total execution time to execute the file a.out to the file Output.log

Upvotes: 2

RedComet
RedComet

Reputation: 1202

Your times, will never be the same, because every single time you execute your program, the conditions on the machine are different, the OS process/thread scheduler, ram assigned and freed again, as well as other task the system might be doing, like reading a heavy file from disk, swapping.

Kurtis Nusbaum already pointed out the meanings of real,user and sys, I'll just clarify real: is the total time to execute the program so if by chance your disk is under heavy operation, the total time will be bigger when your program reads data. So it does NOT measure efficiency but just elapsed time. user + sys time would be a better efficiency meter.

Upvotes: 2

Friedrich
Friedrich

Reputation: 6006

There is no "solution" because there is no problem. You are running a multi tasking system and it depends on the load how fast you get the response. What you can do is running it a few times and take the average as "guide".

Upvotes: 3

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

Each of those are the different runtimes of your program.

  • Real is the actual wallclock time it took to run

  • sys is the amount of time the system spent while executing your program (i.e. time spent by the kernel)

  • user is the amount of time that was spent in user-mode executing it (i.e. time spent excuting where you weren't in the kernel).

Upvotes: 5

Related Questions