Hello everyone! I started by using ifstream to read my model format but found it to be really slow. So I switched to cstdio only to find out that ifstream is based on cstdio so it ran at the same speed. Does anyone know of a better, faster way to read files? It's weird because even C#'s BinaryReader ran faster than cstdio while reading my file. Thanks!
Crossbones+ - Reputation: 13391
Posted 02 April 2014 - 03:06 PM
No matter what interface you use to load a file’s contents it will be slow if your approach to loading is naïve, and streams, while serving to abstract away the source of data (be it from a file, a network, user input, etc.), tend promote naïve loading practices.
Streams, cstdio, ReadFile, and any other API/interface can be fast as long as you know what to do and what to avoid.
Load data in large chunks at a time.
Make as few calls to the API/interface as possible (in other words, load data in large chunks at a time).
Cache files that will be opened regularly.
Read small amounts of data at a time such as single bytes or single integers unless necessary. Streams tend to promote this practice so it is likely what you have done and the cause of your performance issues.
Repeatedly open and close files.
For games, where performance is always critical, the best way to load a file is to map it to memory (CreateFileMapping()) and access it directly rather than going through any API/interface.
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums