About CPU time

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
haaaz
New poster
Posts: 29
Joined: Sun Sep 08, 2002 8:02 am

About CPU time

Post by haaaz »

How can I know how long my program runs?
Do I have to code myself or I can get such information from my compiler.......?

And how about the memory size a program uses?

kmhasan
Problemsetter
Posts: 107
Joined: Fri Oct 26, 2001 2:00 am
Location: Canada
Contact:

Post by kmhasan »

If you're using linux it's trivial, you don't need to do add anything in the code. Given a binary executable a.out and an input file a.in you can use:

Code: Select all

time ./a.out < a.in
For platform independence, I think you can use the functions provided in time.h.
[cpp]
#include <time.h>
#include <stdio.h>

int main() {
clock_t start = clock();
/* do whatever is needed */
clock_t stop = clock();
printf("%.3lfs\n",(double)(stop-start)/CLOCKS_PER_SEC);
return 0;
}
[/cpp]

haaaz
New poster
Posts: 29
Joined: Sun Sep 08, 2002 8:02 am

Post by haaaz »

How about memory usage?

anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

well problemsetter bhai, thanks for your great teaching.
it's very important use.
will it work on cygwin?
will there be any problem to judge if i use clock_t will it return compile error?
---
:oops: :oops:
"Everything should be made simple, but not always simpler"

ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

Post by ec3_limz »

well problemsetter bhai, thanks for your great teaching.
it's very important use.
will it work on cygwin?
will there be any problem to judge if i use clock_t will it return compile error?
I think the functions in time.h are available for any compiler. However, I don't see any point in sending a program using the time functions to the judge.

anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

well a great important question is abt memory usage.
this is bcz it is not possible to calculate total memory from all the variable list whn using a big program.
and many dos compiler don't allow a fixed limit of memory.
so how many variables to allocate in the given menmory is a big ?.
--
if any1 knows please let us know abt it..
"Everything should be made simple, but not always simpler"

Post Reply

Return to “C++”