# C++ Question

**URL:** <https://boards.straightdope.com/t/c-question/164178>\
**Category:** Factual Questions\
**Created:** [March 26, 2003, 1:49am UTC](https://boards.straightdope.com/t/c-question/164178 "2003-03-26T01:49:39Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayray5884](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@rayray5884](https://boards.straightdope.com/u/rayray5884)\
**Post date:** [March 26, 2003, 1:49am UTC](https://boards.straightdope.com/t/c-question/164178/1 "2003-03-26T01:49:39Z")

</div>

I remember hearing about C++ code that, when placed in the program, calculated the total run-time of the program but cannot find it. What I want it for is to be able to test the efficiency of a program using different methods and such. Is there anyone out there that knows what I’m talking about?? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)\
**Post date:** [March 26, 2003, 1:55am UTC](https://boards.straightdope.com/t/c-question/164178/2 "2003-03-26T01:55:01Z")

</div>

It wouldn’t be terribly difficult. Capture the system timer at the start of the program, and compare the stored value to the current system timer at each of the program’s exit points, and output the difference. That’s your run time.

---

<div class="post-metadata">

**Author:** ![Apex\_Rogers](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@Apex\_Rogers](https://boards.straightdope.com/u/Apex_Rogers)\
**Post date:** [March 26, 2003, 1:59am UTC](https://boards.straightdope.com/t/c-question/164178/3 "2003-03-26T01:59:36Z")

</div>

There is a simple way to do this, however I do not know the specific commands. Essentially you do what Q.E.D. described. I wish I would have dug deeper in my programming class, but this was not one of the topics we covered.

---

<div class="post-metadata">

**Author:** ![rayray5884](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@rayray5884](https://boards.straightdope.com/u/rayray5884)\
**Post date:** [March 26, 2003, 2:09am UTC](https://boards.straightdope.com/t/c-question/164178/4 "2003-03-26T02:09:18Z")

</div>

That’s kind of what I thought QED. I guess I’ll just have to search around for the code because I’ve yet to learn how to do that. Thanks!

---

<div class="post-metadata">

**Author:** ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)\
**Post date:** [March 26, 2003, 2:23am UTC](https://boards.straightdope.com/t/c-question/164178/5 "2003-03-26T02:23:21Z")

</div>

It depends on the kind of output you want. The output methods will be different depening if you are programming for a Windows or DOS environment. Basically it will look something like this for a Windows app:

```auto

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPSTR lpCmdLine, int nCmdShow)
{
	TMyApp MyApp("Program_name", hInstance, hPrevInstance,
			lpCmdLine, nCmdShow);
     SetMessageQueue( 8 );
                **Get system time here**
 	MyApp.Run();
                **Return system time - saved system time here**
  	return MyApp.Status;
}

```

The only special variable is the system time, and you can get that from your compiler documentation.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [March 26, 2003, 3:05am UTC](https://boards.straightdope.com/t/c-question/164178/6 "2003-03-26T03:05:09Z")

</div>

#include \<time.h\>

…

time\_t Start = time(NULL);  
…  
time\_t End = time(NULL);

double DiffInSeconds = difftime(End, Start);

---

<div class="post-metadata">

**Author:** ![Joe\_Cool](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@Joe\_Cool](https://boards.straightdope.com/u/Joe_Cool)\
**Post date:** [March 26, 2003, 4:23am UTC](https://boards.straightdope.com/t/c-question/164178/7 "2003-03-26T04:23:46Z")

</div>

Or if you’re using Linux, you can just run your executable as an argument to /usr/bin/time. It will give you output like this:

# time ./test

real 0m0.056s  
user 0m0.025s  
sys 0m0.025s

---

<div class="post-metadata">

**Author:** ![TGWATY](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@TGWATY](https://boards.straightdope.com/u/TGWATY)\
**Post date:** [March 26, 2003, 4:40am UTC](https://boards.straightdope.com/t/c-question/164178/8 "2003-03-26T04:40:45Z")

</div>

**Joe\_Cool** ’s method would also work in HP-UX and most other flavors of UNIX.

---

<div class="post-metadata">

**Author:** ![cmkeller](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cmkeller/32/118_2.png) [@cmkeller](https://boards.straightdope.com/u/cmkeller)\
**Post date:** [March 26, 2003, 5:21am UTC](https://boards.straightdope.com/t/c-question/164178/9 "2003-03-26T05:21:02Z")

</div>

Or, if you’re in some flavor of Windows, the preferred function is GetTickCount(), as follows:

DWORD first\_tick, last\_tick, milliseconds\_elapsed;

first\_tick = GetTickCount();

// Run program code

last\_tick = GetTickCount();

milliseconds\_elapsed = last\_tick - first\_tick;

---

<div class="post-metadata">

**Author:** ![Melraidin](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Melraidin](https://boards.straightdope.com/u/Melraidin)\
**Post date:** [March 26, 2003, 6:01am UTC](https://boards.straightdope.com/t/c-question/164178/10 "2003-03-26T06:01:56Z")

</div>

Don’t forget the high performance timers. Although I don’t have sample code for you, just do a search for it and I’m sure you’ll find some (suggested search terms {Google}: “High performance timer” optimize code). Also, consider looking around game programming forums for more information on optimizing code.

---

<div class="post-metadata">

**Author:** ![ZenBeam](https://avatars.discourse-cdn.com/v4/letter/z/3ab097/32.png) [@ZenBeam](https://boards.straightdope.com/u/ZenBeam)\
**Post date:** [March 26, 2003, 5:53pm UTC](https://boards.straightdope.com/t/c-question/164178/11 "2003-03-26T17:53:24Z")

</div>

This is what I use in a program I happen to have up right now. This is only a little different than what **ultrafilter** has. I never had to use difftime to get the difference.

#include \<time.h\>  
…  
time\_t ltime, ltime0;  
time( &ltime0 );  
// Stuff you want to time goes here.  
time( &ltime );  
cout \<\< " Ellapsed time = " \<\< ltime - ltime0 \<\< " seconds." \<\< endl;  
You can call time( &ltime ); whenever you want a new reading on the ellapsed time.

---

<div class="post-metadata">

**Author:** ![rayray5884](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@rayray5884](https://boards.straightdope.com/u/rayray5884)\
**Post date:** [March 26, 2003, 9:46pm UTC](https://boards.straightdope.com/t/c-question/164178/12 "2003-03-26T21:46:53Z")

</div>

Thanks everyone! You knew exactly what i was looking for!

---

<div class="post-metadata">

**Author:** ![psychonaut](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/psychonaut/32/4655_2.png) [@psychonaut](https://boards.straightdope.com/u/psychonaut)\
**Post date:** [March 26, 2003, 9:58pm UTC](https://boards.straightdope.com/t/c-question/164178/13 "2003-03-26T21:58:59Z")

</div>

To those who suggested code with the line #include \<time.h\> – please note that this is not standard C++. If it works with any given compiler, then it’s simply because that compiler supports this construction as a proprietary extension. The proper way of including the C time library in a C++ program is with the directive

#include \<ctime\>

Using this will ensure that your code works on _any_ conformant C++ compiler.

---

<div class="post-metadata">

**Author:** ![Shalmanese](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Shalmanese](https://boards.straightdope.com/u/Shalmanese)\
**Post date:** [March 27, 2003, 12:22am UTC](https://boards.straightdope.com/t/c-question/164178/14 "2003-03-27T00:22:45Z")

</div>

Why not just run a profiler on it and get a bit more inofrmative output than just the time.

---

<div class="post-metadata">

**Author:** ![NeutronJack](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/neutronjack/32/4134_2.png) [@NeutronJack](https://boards.straightdope.com/u/NeutronJack)\
**Post date:** [March 27, 2003, 6:01am UTC](https://boards.straightdope.com/t/c-question/164178/15 "2003-03-27T06:01:51Z")

</div>

> [@](#):
>
> \*Originally posted by psychonaut \*  
> **To those who suggested code with the line #include \<time.h\> – please note that this is not standard C++.**

The ISO C++ standard, D.5.1, states:

> [@](#):
>
> the C++ Standard library provides the 18 C headers, as shown in Table 100

and the table contains \<time.h\>. It’s a deprecated feature, and you can certainly argue that \<ctime\> is better style, but \<time.h\> **is** standard C++.

---

<div class="post-metadata">

**Author:** ![psychonaut](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/psychonaut/32/4655_2.png) [@psychonaut](https://boards.straightdope.com/u/psychonaut)\
**Post date:** [March 27, 2003, 10:31am UTC](https://boards.straightdope.com/t/c-question/164178/16 "2003-03-27T10:31:59Z")

</div>

OK, yes, you’re technically right, **MrNeutron** , but including \<time.h\> instead of \<ctime\> may result in name clashes. If your program already defines a function time() in the default namespace, then using \<time.h\> will not work. (Of course, it goes without saying that using \<ctime\> mandates use of the std:: prefix when calling its functions.)
