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

14,522

Solution 1

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

Solution 2

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

Solution 3

Look here first: calculating execution time in c++

You can also use a clock (from time.h)in code (although for multi-threaded code it works kinda funny)

int a;
unsigned t0 = clock(), t1;
std::cin >> a;
t1 = clock() - t0;
Share:
14,522
antonionikolov
Author by

antonionikolov

Updated on June 21, 2022

Comments

  • antonionikolov
    antonionikolov almost 2 years

    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!