gpt4 book ai didi

c++ - 添加额外的 header 会使程序变慢吗?

转载 作者:行者123 更新时间:2023-11-30 02:22:50 25 4
gpt4 key购买 nike

例如下面两个程序是否有保存执行时间?

#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;
}

如果是这样,总是包含一堆头文件是一个好习惯吗?如何测试执行一个程序需要多长时间?使用预定义的输入。

最佳答案

Does adding additional headers make programs slower?

没有。当然,现在有人会带着一些极端案例来反驳这一点。但不会,额外的 header 通常不会使 C 或 C++ 程序变慢。

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

不要包括“一堆”。包括你使用的那些。包含额外的 header 会增加编译时间,如果 header 来自您自己的项目,则可能会导致在您触摸任何 header 时重新编译项目中的许多对象。

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

用秒表。或者 time()。或者 rdtsc。或者 QueryPerformanceCounter()。很多方法。

关于c++ - 添加额外的 header 会使程序变慢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46862799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com