【发布时间】:2021-12-26 23:47:47
【问题描述】:
我正在使用这个命令行来编译我的程序。
clang++ -std=c++17 -O3 main.cpp -o main
我在 20 分钟前启动了编译器,它只是挂起。我终止了编译器,并尝试再次编译它,它仍然挂起。如果我使用完全相同的命令行,但没有 -O3,编译器会立即完成,但使用 -O3 它会挂起。
它正在编译的代码比较简单,没有任何错误。怎么回事?
#include <ctime> // for time()
#include <cstdlib> // for srand(), rand(), size_t, EXIT_SUCCESS
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
vector<string> messages;
messages.push_back(string("“Blessed are those who are persecuted because of righteousness, for theirs is the kingdom of heaven.”"));
messages.push_back(string("“Let the little children come to me, and do not hinder them, for the kingdom of heaven belongs to such as these.”"));
/* Literally 10000 more quotes from the Bible. */
srand(time(NULL));
cout << messages[ rand() % messages.size() ] << endl;
return EXIT_SUCCESS;
}
发生了什么事?
【问题讨论】:
-
Literally 10000 more quotes from the Bible你添加了这么多引号,编译器需要很长时间才能编译。 -
@KamilCuk 如何让编译器运行得更快?
-
你能从文件中读出这些引用吗?真的没必要把一万个(!)字符串直接放到源代码中
-
你可以让它更高效一点。您必须考虑,编译器必须为所有这些输出代码,然后您要求它尽可能积极地对其进行优化。如果您坚持按照自己的方式进行操作,请使用向量构造函数而不是推回。 en.cppreference.com/w/cpp/container/vector/vector 我也不会使用字符串,无论是 string_view 还是 const char*
-
@Galaxy 你试过我建议的方法了吗?它对我来说非常有效。