如何优化此C++;? 我试图通过做一些旧的谷歌代码阻塞问题来练习C++。我发现一个相对简单的方法是将字符串中的单词颠倒过来。可以在这里找到
到目前为止,我已经:如何优化此C++;? 我试图通过做一些旧的谷歌代码阻塞问题来练习C++。我发现一个相对简单的方法是将字符串中的单词颠倒过来。可以在这里找到,c++,optimization,g++,pypy,C++,Optimization,G++,Pypy,到目前为止,我已经: #include<iostream> using namespace std; int main(){ int n = 0; cin >> n; string rev = ""; string buf = ""; string data = ""; getline(cin, data); for(int _ = 0; _ < n; _++){ getline(ci
#include<iostream>
using namespace std;
int main(){
int n = 0;
cin >> n;
string rev = "";
string buf = "";
string data = "";
getline(cin, data);
for(int _ = 0; _ < n; _++){
getline(cin, data);
rev = "";
buf = "";
for(char& c : data) {
buf += c;
if(c == ' '){
rev = buf + rev;
buf = "";
}
}
cout << "Case #" << _ + 1 << ": " << buf << " " << rev << endl;
}
return 0;
}
但是,当我在pypy
下运行它时,在time pypy reverse.py<
理论上,如<代码> PyPy <代码>是用C++编写的,C++不应该是快的还是快的,如果是这样,这个代码怎么能被优化得更快?< /P> < P>我认为当你连接字符串时,你的C++代码做了很多内存拷贝(大多数STD::字符串的实现将整个字符串保持在内存中)。我认为下面的代码没有拷贝就可以做到这一点,但我没有对它进行太多测试。至于python为什么表现得很好,我不完全确定
#include<iostream>
int main()
{
size_t numCases;
std::cin >> numCases;
std::cin.ignore();
for( size_t currentCase = 1; currentCase <= numCases; ++currentCase )
{
std::cout << "Case #" << currentCase << ": ";
std::string line;
getline(std::cin, line);
size_t wordEnd = line.length() - 1;
size_t lastSpace = std::string::npos;
for ( int pos = wordEnd - 1; pos >= 0; --pos )
{
if ( line[pos] == ' ' )
{
for ( int prt = pos + 1; prt <= wordEnd; ++prt )
std::cout << line[prt];
std::cout << ' ';
lastSpace = pos;
wordEnd = pos - 1;
--pos;
}
}
for ( int prt = 0; prt < lastSpace; ++prt )
std::cout << line[prt];
std::cout << std::endl;
}
return 0;
}
#包括
int main()
{
大小和大小;
性病::cin>>核酶;
std::cin.ignore();
对于(size_t currentCase=1;currentCase而不是使用两个缓冲区和大量的串联,您可以利用算法和迭代器库来实现更简单的操作。我不确定它会快多少(虽然我猜会很快),但它也更紧凑
#include<iostream>
#include<algorithm>
#include<iterator>
#include<sstream>
using namespace std;
int main(){
int n = 0;
cin >> n;
string data = "";
getline(cin, data);
for(int _ = 0; _ < n; _++){
getline(cin, data);
stringstream ss(data);
reverse(istream_iterator<string>(ss), istream_iterator<string>());
cout << "Case #" << _ + 1 << ": " << ss.str() << endl;
}
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
int n=0;
cin>>n;
字符串数据=”;
getline(cin,数据);
对于(int=0; cout一个简单的不复制/不分配的标记器是令人讨厌的
在我的测试中,以下内容胜过了您的python程序
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
int main()
{
std::cout.sync_with_stdio(false); // we don't need C in the picture
std::string line;
getline(std::cin, line);
int num_cases = stoi(line);
std::vector<char*> words;
for(int n = 0; getline(std::cin, line) && n < num_cases; ++n)
{
words.clear();
char* p = std::strtok(&line[0], " ");
while (p) {
words.push_back(p);
p = std::strtok(nullptr, " ");
}
std::cout << "Case #" << n + 1 << ": ";
reverse_copy(words.begin(), words.end(),
std::ostream_iterator<char*>(std::cout, " "));
std::cout << '\n'; // never std::endl!
}
}
#包括
#包括
#包括
#包括
#包括
int main()
{
std::cout.sync_with_stdio(false);//图片中不需要C
std::字符串行;
getline(标准::cin,line);
int num_cases=stoi(行);
向量词;
对于(int n=0;getline(std::cin,line)和&n std::你真的不应该用“\ux”吗作为一个变量名,如果没有其他东西,只是作为一种样式,但以u或u_u开头的变量对某些编译器通常有特殊的意义。@以下划线后跟大写字母开头的标识符和包含双下划线的标识符是为实现保留的。这适用于所有编译器。谢谢r告诉我。我想我最初使用它是因为我认为我不需要它,我猜这只是用python编写代码的一个习惯,如果在for循环中有一个不需要的变量,我发现大多数情况下只需将其称为“\ux”不管怎样,改变它似乎对时间没有太大的影响。如果你想要真正的好性能,放弃C++的IO东西和字符串……普通的C,char,Maloc……在编译< <代码> G++-O3时,运行<代码>时间时,这看起来似乎比较慢。/Reutial//DeV/NULL/<代码>,大约需要4.5秒。为了编译你的代码,我得到了:“错误:SS”在这个范围内没有声明。(包括:对不起,关于代码!>代码> 1.2代码/代码>秒。这很快!我不知道STD::Strutok,但是它肯定能很好地执行这个过程。谢谢。我还认为我的C++和Python代码做了同样的事情。它们到底有什么不同?”每行末尾的空格
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
int main()
{
std::cout.sync_with_stdio(false); // we don't need C in the picture
std::string line;
getline(std::cin, line);
int num_cases = stoi(line);
std::vector<char*> words;
for(int n = 0; getline(std::cin, line) && n < num_cases; ++n)
{
words.clear();
char* p = std::strtok(&line[0], " ");
while (p) {
words.push_back(p);
p = std::strtok(nullptr, " ");
}
std::cout << "Case #" << n + 1 << ": ";
reverse_copy(words.begin(), words.end(),
std::ostream_iterator<char*>(std::cout, " "));
std::cout << '\n'; // never std::endl!
}
}