5

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!

antonionikolov
  • 97
  • 1
  • 1
  • 8
  • 2
    Instead say `time ./file` and parse the output as per your needs. – devnull Mar 02 '14 at 20:56
  • The shell captures the exit status in the `$?` variable, so `echo $?` shows you the exit status of the most recently executed command. But I don't know how to do it on the Windows command prompt. – Brian Bi Mar 02 '14 at 20:58

4 Answers4

10

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
benedek
  • 396
  • 1
  • 6
8

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

Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
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;
Community
  • 1
  • 1
turnt
  • 3,235
  • 5
  • 23
  • 39
0

Read carefully time(7). You may use time(1) externally (from your shell, like answered here). You could also use from inside your program syscalls like clock_gettime(1) etc...

Don't expect timing to be really meaningful or accurate for small delays (e.g. less than half a second).

BTW, your question has not much to do with the GCC compiler (i.e. g++). If using a recent GCC 4.8 with -std=c++11 you could use std::chrono etc etc... And even if using these it is not the compiler which is timing your program (but some library functions using syscalls)

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547