C++ ifstream::read slow due to memcpy

Recently I decided to optimize some file reading I was doing, because as everyone says, reading a large chunk of data to a buffer and then working with it is faster than using lots of small reads. And my code certainly is much faster now, but after doing some profiling it appears memcpy is taking up a lot of time. The gist of my code is... ifstream file("some huge file"); char buffer[0x100000

由于memcpy,C ++ ifstream :: read变慢

最近我决定优化一些我正在做的文件阅读,因为正如大家所说,将大量数据读取到缓冲区然后使用它比使用大量小读取更快。 而且我的代码现在肯定快得多,但是在进行一些分析之后,似乎memcpy占用了大量时间。 我的代码的要点是... ifstream file("some huge file"); char buffer[0x1000000]; for (yada yada) { int size = some arbitrary size usually around a megabyte; file.read(buffer, size); //Do stuff with

Which is the fastest method of input in C++

I am coding for various programming olympiads and thus to improve time efficiency i am looking for the fastest method for getting input without need of adding extra library , ie , within the standard available directory as per gcc compilers as during the competitions we obviously cant add libraries to compiler . Till now in C++ , i used cin and cout , but found that scanf and printf are much fas

这是C ++中最快的输入方法

我为各种编程奥林匹克编码,从而提高时间效率,我正在寻找最快的方法获取输入,而不需要添加额外的库,即在标准可用目录内按照gcc编译器在比赛期间,我们显然不能添加库编译器。 直到现在我用C ++,我用cin和cout,但发现scanf和printf比它快得多,所以任何人都可以告诉我更快的方法,这可能是一个自定义函数,因为我不介意空间的复杂性,但我更喜欢时间。 Thankyou先进。 这些流总是比C-API函数慢,这是一个非常常见的误解

stdio vs iostream

When I search on the internet for the difference between these two libraries, everyone says that <iostream> is the standard I/O library of C++ and <cstdio> is for C. My professor says that cin>> and cout<< are not good functions and if we use cin>> many times our application will definitely crash. He also says that stdio provides nearly 3 times faster input and outp

stdio vs iostream

当我在互联网上搜索这两个库的区别时,大家都说<iostream>是C ++的标准I / O库, <cstdio>是C的。我的教授说cin>>和cout<<好的功能,如果我们使用cin>>很多时候我们的应用程序肯定会崩溃。 他还表示, stdio提供比iostream快3倍的输入和输出。 不过,我更喜欢使用iostream因为它更方便,而且我不知道我的教授是否正确。 那么你建议我使用什么? 使用iostream不应该让你的程序崩溃。 它可能

Is std::ifstream significantly slower than FILE?

I've been informed that my library is slower than it should be, on the order of 30+ times too slow parsing a particular file (text file, size 326 kb). The user suggested that it may be that I'm using std::ifstream (presumably instead of FILE ). I'd rather not blindly rewrite, so I thought I'd check here first, since my guess would be the bottleneck is elsewhere. I'm readin

std :: ifstream明显比FILE慢吗?

我被告知,我的库比应该慢,大约30+次解析特定文件(文本文件,大小为326 kb)的速度太慢。 该用户建议,可能是我使用std::ifstream (大概而不是FILE )。 我宁愿不要盲目地重写,所以我想我会先在这里检查,因为我的猜测会成为其他地方的瓶颈。 我正在tellg()/seekg()阅读字符,所以我使用的唯一函数是get() , peek()和tellg()/seekg() 。 更新: 我进行了描述,并且输出了令人困惑的结果 - gprof似乎并不认为花了这

Why are std::fstreams so slow?

I was working on a simple parser and when profiling I observed the bottleneck is in... file read! I extracted very simple test to compare the performance of fstreams and FILE* when reading a big blob of data: #include <stdio.h> #include <chrono> #include <fstream> #include <iostream> #include <functional> void measure(const std::string& test, std::function<

为什么std :: fstreams很慢?

我正在研究一个简单的解析器,当分析时我观察到瓶颈在...文件读取! 我提取非常简单的测试来比较读取大量数据时fstreams和FILE*的性能: #include <stdio.h> #include <chrono> #include <fstream> #include <iostream> #include <functional> void measure(const std::string& test, std::function<void()> function) { auto start_time = std::chrono::high_resolution_clock::

Why is istream/ostream slow

At 50:40 of http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly Andrei Alexandrescu makes a joke about how not efficient/slow istream is. I had an issue in the past with ostream being slow and fwrite being significantly faster (reducing many seconds when running the main loop once) but I never understood why nor looked into it. What makes istream and ostream slo

为什么istream / ostream很慢

在http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-快速50:40 Andrei Alexandrescu开了一个关于如何不高效/缓慢的istream的笑话。 我以前遇到过一个问题,ostream速度很慢,而且fwrite的速度要快得多(在运行主循环时会减少很多秒),但我从来不理解为什么也不研究它。 是什么让C ++中的istream和ostream变慢? 或者至少比其他的东西(比如fread / fget,fwrite)慢得多,这些东西同样会满足

separated floats in C++ quickly?

I have a file with millions of lines, each line has 3 floats separated by spaces. It takes a lot of time to read the file, so I tried to read them using memory mapped files only to find out that the problem is not with the speed of IO but with the speed of the parsing. My current parsing is to take the stream (called file) and do the following float x,y,z; file >> x >> y >>

用C ++快速分离花车?

我有一个有数百万行的文件,每行有三个浮动空格分隔。 读取文件需要很长时间,所以我试图使用内存映射文件来读取它们,以发现问题不在于IO的速度,而在于解析速度。 我目前的解析是采取流(称为文件)并执行以下操作 float x,y,z; file >> x >> y >> z; 有人在堆栈溢出推荐使用Boost.Spirit,但我找不到任何简单的教程来解释如何使用它。 我试图找到一个简单而有效的方法来解析看起来像这样的一行: "1

Why do std::string operations perform poorly?

I made a test to compare string operations in several languages for choosing a language for the server-side application. The results seemed normal until I finally tried C++, which surprised me a lot. So I wonder if I had missed any optimization and come here for help. The test are mainly intensive string operations, including concatenate and searching. The test is performed on Ubuntu 11.10 a

为什么std :: string操作表现不佳?

我做了一个测试,比较几种语言的字符串操作,以选择服务器端应用程序的语言。 结果似乎很正常,直到我终于尝试C ++,这让我感到很惊讶。 所以我想知道我是否错过了任何优化并来这里寻求帮助。 测试主要是密集的字符串操作,包括连接和搜索。 该测试在Ubuntu 11.10 amd64上进行,并使用GCC的4.6.1版本进行。 该机器是戴尔Optiplex 960,具有4G RAM和四核CPU。 在Python(2.7.2)中: def test(): x = "" limit =

Why is scanf/printf faster than cin/cout?

This question already has an answer here: Why is reading lines from stdin much slower in C++ than Python? 10 answers Accessing individual characters in a file inefficient? (C++) 3 answers

为什么scanf / printf比cin / cout更快?

这个问题在这里已经有了答案: 为什么读取stdin中的行比C ++慢得多? 10个答案 访问文件中的单个字符效率低下? (C ++)3个答案

Python faster than C++? How does this happen?

This question already has an answer here: Why is reading lines from stdin much slower in C++ than Python? 10 answers There isn't anything obvious here. Since Python's written in C, it must use something like printf to implement print . C++ I/O Streams, like cout , are usually implemented in a way that's much slower than printf . If you want to put C++ on a better footing, you

Python比C ++更快? 这是如何发生的?

这个问题在这里已经有了答案: 为什么读取stdin中的行比C ++慢得多? 10个答案 这里没有什么明显的。 由于Python是用C编写的,它必须使用类似printf东西来实现print 。 C ++ I / O流,如cout ,通常以比printf慢得多的方式实现。 如果你想把C ++放在更好的基础上,你可以尝试改变: #include <cstdio> int main() { int x=0; while(x!=1000000) { ++x; std::printf("%dn", x); }