Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make g++ to show the execution time in terminal?

Tags:

c++

linux

g++

I want to make g++ showing me what is the execution time and maybe the return too.

g++ file.cpp -o file
./file

When i make the executable file and then call in it is showing only the output without the return and execution time.

And i want to make it showing something like this:

Process returned 0 (0x0)    execution time : 0.002 s

Thank you for the attention!

like image 685
antonionikolov Avatar asked Dec 01 '22 16:12

antonionikolov


2 Answers

You can measure how long your process takes with the "time" command.

To determine the return value, you can print the value of the $? environment variable after running your program:

time ./file ; echo Process returned $?

You can also specify how exactly time should format its results with the -f (or --format) option. However, some Linux distributions might use a bash-builtin time implementation by default which lacks that option, so you might have to give the full path to use the real time program:

/usr/bin/time -f "Execution time: %E" ./file
like image 198
benedek Avatar answered Dec 15 '22 15:12

benedek


You can use time command as follows:
time ./file

like image 44
Shashwat Kumar Avatar answered Dec 15 '22 15:12

Shashwat Kumar