我想测试在bash脚本和C++程序中写入文件的性能.
这是bash脚本:
#!/bin/bash
while true; do
echo "something" >> bash.txt
done
Run Code Online (Sandbox Code Playgroud)
这每秒向文本文件添加大约2-3 KB.
这是C++代码:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream myfile;
myfile.open("cpp.txt");
while (true) {
myfile << "Writing this to a file Writing this to a file \n";
}
myfile.close();
}
Run Code Online (Sandbox Code Playgroud)
这在不到10秒的时间内创建了一个~6 GB的文本文件.
是什么让这个C++代码更快,和/或这个bash脚本这么慢?
我已经阅读了三种方法,可以从各种来源以c ++的形式将内容打印到控制台.
using namespace std;然后使用cout(CodeBlocks标准版)std::cout和std::endl;(C++ Primer)printf(HackerRank)哪个是首选,为什么?
我实际上是C++的粉丝,但今天我发现我的程序的文件输出很慢.所以,我设计了一个实验来比较C++文件输出的速度和C.假设我们有这段代码:
int Num = 20000000;
vector <int> v;
for ( int i = 0; i < Num; i++ )
{
v.push_back(i);
}
Run Code Online (Sandbox Code Playgroud)
现在我运行两个单独的代码,一个用C++编写:
int now = time(0);
cout << "start" << endl;
ofstream fout("c++.txt");
for(size_t i = 0; i < v.size(); ++i)
{
fout<< v[i] << endl;
}
fout.close();
cout << time(0) - now << endl;
Run Code Online (Sandbox Code Playgroud)
和一个在C:
int now = time(0);
printf("start\n");
FILE *fp = fopen("c.txt", "w");
for(size_t i = 0; i < v.size(); ++i)
{
fprintf(fp, "%d\n", v[i]);
} …Run Code Online (Sandbox Code Playgroud) 考虑UNIX,Windows和Mac以及输出流(二进制和文本),
什么std::endl代表,即<CR><LF>,<LF>或<CR>?或者无论平台/编译器是什么,它总是一样的吗?
我问的原因是我正在编写一个TCP客户端,该客户端会讨论一个期望每个命令结束的协议<CR><LF>.所以我想知道是否使用std::endl或"\r\n"在我的流中.
编辑:好的,所以一个冲洗缓冲区而另一个没有.我明白了.但是,如果我将文本输出到文件,'\n'等于<LF>或者是否<CR><LF>在Windows上和<LF>Unix 上进行转换?我还没有看到明确的答案.
可能重复:
C++:"std :: endl"vs"\n"
在Accelerated C++中,提到了两件事:
大多数系统花费大量时间将字符写入输出设备.因此,C++会累积要写入缓冲区的字符,并等待刷新缓冲区.
刷新缓冲区的一种方法是,如果我们通过使用明确告诉它这样做std::endl.
这使我想知道:很明显的好处是非常的一切小,不易察觉,除了最大的产出,但使用"\n"比使用速度更快std::endl,或者根本"\n"也刷新缓冲区?
考虑:
#include <iostream>
using namespace std;
class A {// base class
private:
int data;
public:
A(int data = 0)
{
this->data = data;
}
void show()
{
cout << data << endl;
return;
}
};
class B : virtual public A {
public:
B(int data = 0) :
A(data) {
}
};
class C : virtual public A {
public:
C(int data = 0) :
A(data) {
}
};
class D : public B, public C {
public:
D(int dataB …Run Code Online (Sandbox Code Playgroud) 我正在Linux平台上编写一个程序,该程序生成的文本文件将不可避免地在Windows平台上进行查看.
现在,传入std::endla ostream仅为换行生成CR字符.当然,这些文本文件在MS记事本中看起来不对.
std::endl它使用CR + LF换行而不是LF?win_endl,用于生成我自己的换行符,但我std::endl在很多地方使用该符号,并且像许多程序员一样,倾向于做需要尽可能少工作的事情.我可以简单地重载std::endl以产生CR + LF,或者这对可维护性来说是一个愚蠢的想法?注意:我查了一下这个问题,但是它问的是另一种方式,接受的答案似乎相当不完整.
我的机器正在运行ubuntu 10.10,我正在使用标准的gnu C库.我的印象是,如果格式字符串中描述了换行符,printf会刷新缓冲区,但是下面的代码反复出现了这种趋势.有人可以澄清为什么缓冲区没有被刷新.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
int main()
{
int rc;
close(1);
close(2);
printf("HI 1\n");
fprintf(stderr, "ERROR\n");
open("newfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0600);
printf("WHAT?\n");
fprintf(stderr, "I SAID ERROR\n");
rc = fork();
if (rc == 0)
{
printf("SAY AGAIN?\n");
fprintf(stderr, "ERROR ERROR\n");
}
else
{
wait(NULL);
}
printf("BYE\n");
fprintf(stderr, "HI 2\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行该程序后newfile.txt的内容如下.
HI 1
WHAT?
SAY AGAIN?
BYE
HI 1
WHAT?
BYE
Run Code Online (Sandbox Code Playgroud) 这是代码
#include<iostream>
using namespace std;
main()
{
cout<<"Hellow World"<<endl;
cout.operator<<("Hellow World");
cout.operator<<(endl);
}
Run Code Online (Sandbox Code Playgroud)
我知道这cout<<"Hellow World"<<endl;被解释为
cout.operator<<("Hellow World");
但是这段代码产生了以下结果
Hellow World
0x8048830
如果我使用operator<<(cout,"Hellow World");工作正常cout.operator<<("Hellow World");和
之间的区别 operator<<(cout,"Hellow World");
我正在计算将文本打印到标准输出的各种方法之间的差异.我正在测试cout,printf并ostringstream使用\n和std::endl.我希望std::endl能有所作为cout(并且确实如此),但我没想到它会减慢输出速度ostringstream.我以为使用std::endl只会写一个\n流,它仍然只会被刷新一次.这里发生了什么?这是我的所有代码:
// cout.cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10000000; i++) {
cout << "Hello World!\n";
}
return 0;
}
// printf.cpp
#include <stdio.h>
int main() {
for (int i = 0; i < 10000000; i++) {
printf("Hello World!\n");
}
return 0;
}
// stream.cpp
#include <iostream>
#include <sstream>
using namespace …Run Code Online (Sandbox Code Playgroud) c++ ×9
c ×2
linux ×2
performance ×2
bash ×1
flush ×1
inheritance ×1
io ×1
newline ×1
optimization ×1
ostream ×1
posix ×1
stl ×1
stringstream ×1
windows ×1