1

For instance would the following two programs have the save execution time?

#include <iostream>
int main()
{
int a,b;
std::cin >> a >> b;
std::cout << a+b;
return 0;
}

and

#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
int a,b;
std::cin >> a >> b;
std::cout << a+b;
return 0;
}

If so is it a good practice to always include a bunch of header files? How can one test how long it takes to execute a program? Using predefined input.

  • 5
    If it had been a good idea to include everything, we would have had `#include ` as a standard command. – Bo Persson Oct 21 '17 at 11:40
  • Probably it's good idea to read about how linker is working to avoid such confusions about influence of include on run-time performance – Stepan Novikov Oct 21 '17 at 11:45
  • 1
    @StepanNovikov Did you mean _how the preprocessor is working_? Header files don't have to do with the linking process so much. – user0042 Oct 21 '17 at 11:53
  • If this would be the case then using your compiler's precompiled header file option would be quite unpopular. It is popular. – Hans Passant Oct 21 '17 at 12:04

4 Answers4

4

Does adding additional headers make programs slower?

No. Of course, someone will show up now with some corner case to refute this. But no, extra headers don't make C or C++ programs slower in general.

If so is it a good practice to always include a bunch of header files?

Don't include "a bunch." Include the ones you use. To include extra headers increases compilation time, and if the headers are from your own project, can result in recompiling many objects in your project whenever you touch any header.

How can one test how long it takes to execute a program?

With a stopwatch. Or time(). Or rdtsc. Or QueryPerformanceCounter(). Lots of ways.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I'm looking forward to the corner case too - you cynic :-) – Mark Setchell Oct 21 '17 at 11:39
  • 2
    @MarkSetchell: I'll give you an example from a question I posted here: https://stackoverflow.com/questions/26052640/why-does-gcc-implement-isnan-more-efficiently-for-c-cmath-than-c-math-h - in GCC if you `#include ` before `#include `, you get a slower version of `isnan()`. Of course this is a bug, but it's true. – John Zwinck Oct 21 '17 at 11:42
  • @MarkSetchell if you include `` there are a bunch of global variables being initialized before main starts (`cin`, `cout`, etc.). Theoretically that's extra code to execute compared to if you hadn't included the headers. I am sure however that is so insignificant it can't even be reliably measured. – bolov Oct 21 '17 at 13:26
2

For instance would the following two programs have the same execution time?

Yes. Including additional header files doesn't affect the execution time of the program.

Header files are processed at compile time. So they (usually) don't slow down your code.

There could be corner cases, that inclusion of particular headers might pickup a different implementation of some algorithm, that is inherently slower than a different one picked up without that header.

If so is it a good practice to always include a bunch of header files?

No. You should include the header file for every type you are using, no more no less.

How can one test how long it takes to execute a program? Using predefined input.

There are several possibilities to do that. You can run your program in a profiling tool, or simply measure time yourself (in a script or such).

user0042
  • 7,917
  • 3
  • 24
  • 39
1

Does adding additional headers make programs slower? For instance, would the following two programs have the same execution time?

Adding additional headers won't affect the runtime of your program. It will affect the compile time however because the compiler now has to include these additional headers in your program.

If so is it a good practice to always include a bunch of header files?

It is best practice to only include the header files that you will be using in your project. Also, be careful not to include the C version of a header and the C++ version of a header, you may run into issues.

How can one test how long it takes to execute a program? Using predefined input.

I'd recommend checking out the ctime library: http://www.cplusplus.com/reference/ctime/

Remember that execution time is specific to your machine.

David
  • 79
  • 1
  • 13
-4

I think it will make the program slow because when ever you call a function like cout or cin compiler will find it in header files declared by programmer

More number of header files require more time to find the function definition

Also if including extra header files doesn't increase compilation time then ide(Integrated development environment) should have omit the header file including system

Hope that make sense

BlindCoder
  • 3
  • 1
  • 7
  • 1
    _"Hope that make sense"_ Sorry, no. Not remotely. – user0042 Oct 21 '17 at 11:55
  • 1
    Please don't guess answers. – Bathsheba Oct 21 '17 at 12:01
  • I will guess :-) I guess that @BlindCoder meant that including multiple headers can increase ***compile time***, which it can; but, it ***can not*** increase run time (unless, as noted elsewhere, you have multiple copies of a function and ignore the compiler and linker warnings and the linker just picks the first one found). Best advice 1) include only what you need 2) every file should include what it needs, and not rely on others to do so, which means that 3) every file, including header files, should be compilable (preferably with no warnings) – Mawg says reinstate Monica Oct 21 '17 at 12:12