Sign in to follow this  
Dave1024

gcc function for find execution time

Recommended Posts

Dave1024    122
Hi, i am posting this thread for know the gcc compiler supported time functions for taking the program execution time in second and in millisecond. I am writing my code in ANSI C . Doing some C general optimization i want to check the time difference while execute. what are the functions to find the time in seconds and milliseconds which is accepted by gcc C compiler Rgds Dave

Share this post


Link to post
Share on other sites
phresnel    953
In case you are using MSYS or if you are you on Linux, use the time-command -> g++ my-code.cpp ; time ./a.out.

You can also use std::clock(), std::time() or even OpenMP's wallclock.

(note that all those are not specfic to gcc)

Share this post


Link to post
Share on other sites
Zahlman    1682
These things do not depend on your compiler. They do depend on the rest of your platform: operating system, whether you are making a console or windowed project, etc.

Share this post


Link to post
Share on other sites
phresnel    953
Quote:
Original post by Dave1024
Thanks to your replay !

Is there any significant change in the performence when we use window than Console ?


Now that you seem to know how to check rough performance, maybe test yourself (the answer depends on what exactly you do in the windowed app, if you do not something not so good, then it is negligible)

Share this post


Link to post
Share on other sites
phresnel    953
On the other hand and in all honesty, when you ask questions like "how to measure time", then it is in 99% of cases (with 1% standard error) way too soon to struggle with optimization.

I do not want to de-illusionise you, which optimization settings did you use upon compiling? And if none, then it might be hard for you to recognize that you could have achieved the same effect by simply increasing the optimization level. Modern compilers are more intelligent than most human beings.

What was the code and how did you optimize it?

Share this post


Link to post
Share on other sites
hiigara    108
Nowadays people use

clock_gettime ( CLOCK_REALTIME, & o_timespec )

on Linux for high resolution timing.
gettimeofday() I believe, also gives you a precision to microseconds or nanoseconds.
Just surround your code with 2 calls to clock_gettime().

Share this post


Link to post
Share on other sites
enunes    123
Hi,

on Linux you can just run your program with the time program. Something like:

1. compile your program normally
2. run "time" followed by your program (and also followed by your program's arguments if applicable)


gcc -o prog main.c
time prog


Also, recently while trying to optimize some implementations (which was finished and working, btw) I discovered the gprof tool.

Compiling with gcc -pg, after running your tool, it will generate a file containing stuff like: how much time it took to execute each function call, which was the most demanding function call, which functions took most % time of the execution time at all. I must say my life changed after discovering this tool. :)

It goes like:

1. compile your program with the -pg gcc option
2. run your program normally, might take a bit more of time because it will be taking that time information while it runs
3. run gprof, it will will read a file standardly called gmon.out (which is generated when you run your -pg compiled program) and output a human-readable text containing the information I mentioned before. The ">" will redirect that output to the test1.prog textfile which you can then open with your favourite text editor)


gcc -o prog main.c -pg
./prog
gprof prog > test1.prof

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this