Time of execution

May 4, 2008 at 9:19pm
My program multiplies two numbers given by the user. I would like to see how the time required to do so increases as the numbers increase, so i may plot the working capacity of my laptop processor. Is there a built in function i can use?

Any help will be greatly appreciated.
May 4, 2008 at 9:23pm
In windows. GetTickCount() will give you accuracy down to the millisecond.

If you want more accuracy you can use the Frequency Clock. This is used in game development to workout the time between frames.
May 4, 2008 at 10:15pm
Thank you for the reply.

As i have just started to learn to program, could you tell me how to use the GetTickCount() or the Frequency Clock?

I would prefer the Frequency Clock method for the higher accuracy. thanks
May 4, 2008 at 10:38pm
I don't have code on me (at work) for the clock. But if you drop me an email to public01 _at_ zaita.com I can reply with some code once I get home from work.

As for the tickcount.

1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>

int main() {
int iStartTime = GetTickCount();
// Do Stuff
int iEndTime = GetTickCount();
int iDiff = iEndTime - iStartTime;
cout << "Took " << iDiff << "ms" << endl;

return 0;
}

May 4, 2008 at 11:37pm
Thanks for the reply. When i put the code in theres an error generated saying

"Cannot open include file: 'windows.h': No such file or directory"

How do i solve this? thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <windows.h>
using namespace std; 

int main() {
		int number1;
		int number2;
		int mult;

		cin>>number1>>number2;
		cin.get();	
	
	int iStartTime = GetTickCount();
	mult=number1*number2;

int iEndTime = GetTickCount();
int iDiff = iEndTime - iStartTime;
cout << mult << "\n" ;
cout << "Took " << iDiff << "ms" << endl;

return 0;
}
May 4, 2008 at 11:59pm
Are you running this on Windows or *nix?

That code is for Windows platforms only.

For *nix you will need to use other functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  time_t start_time;
  start_time = time(NULL);

  double elapsed_time = static_cast<double>(time(NULL)-start_time);
  if(elapsed_time < 60) {
    int P = (int) floor(log10(elapsed_time))+4;
    cout << "Total elapsed time: " << std::setprecision(P) << elapsed_time << (elapsed_time==1?" second":" seconds") << endl;
  } else if((elapsed_time/60.0) < 60) {
    elapsed_time /= 60.0;
    int P = (int) floor(log10(elapsed_time))+4;
    cout << "Total elapsed time: " << std::setprecision(P) << elapsed_time << (elapsed_time==1?" minute":" minutes") << endl;
  } else {
    elapsed_time /= 3600.0;
    int P = (int) floor(log10(elapsed_time))+4;
    cout << "Total elapsed time: " << std::setprecision(P) << elapsed_time << (elapsed_time==1?" hour":" hours") << endl;
  }


That code should work on both Windows and *Nix. It's just more complicated.
May 5, 2008 at 12:06am
I am running the code on Windows XP. Can you tell me how to make the simple version work as i do not want my code to become cluttered. Could you also post the Frequency Clock method once u have time? Thanks
May 5, 2008 at 12:10am
I will post frequency stuff when I get home tonight (it's 1pm here now).

What compiler are you using? You should be able to just use GetTickCount() without any include. Or include windows.h
May 5, 2008 at 12:23am
I am using Visual C++ 6 2005 Express Edition. Without the windows.h the compiler gives the errors:

1>.\source.cpp(13) : error C3861: 'GetTickCount': identifier not found
1>.\source.cpp(16) : error C3861: 'GetTickCount': identifier not found

Thanks for your help. its very appreciated.
May 5, 2008 at 12:49am
Turns out you need to download and install some more components before you can use WindowsAPI functions.

http://www.gidforums.com/t-16700.html

http://www.codeproject.com/KB/applications/FreeVS2005Win32.aspx

Once you have installed it view one of those links (i'd use the 2nd one IMO) then you should have no problems using #include <windows.h>

Z.
May 5, 2008 at 3:54am
I'm not sure if this will help as precisely as you like, but a simple way would be to use the time.h library. I don't know what wouldn't make it precise, but, then, I'm no expert yet. ;P''

http://www.cplusplus.com/reference/clibrary/ctime/

I know one of those functions returns time elapsed since program start til the function call, and there are others that can be used to get the differences of time.

Jun 12, 2008 at 3:26pm
There is a function gettimeofday() in standart library (time.h), that returns structure, with seconds and microseconds.

1
2
3
4
5
6
7
8
9
#include <sys/time.h>
#include <time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);

struct timeval {
               time_t         tv_sec;        /* seconds */
               suseconds_t    tv_usec;  /* microseconds */
       };


timezone is not used, put NULL when invoke
Topic archived. No new replies allowed.