File I/O

Started by
4 comments, last by level10boy 22 years, 10 months ago
Does anyone know what the fastest possible file I/O technique is available to a C/C++ programmer. Everyone I speak to seems to have different opinions on the matter but from what I gather, C''s stream I/O is the winner. If anyone can back this up or knows better than me, please reply to me and you will be forever in my debt. Thankz level10boy
Advertisement
Whatever proprietary mechanism the OS provides is likely to have the overall best performance.

... I don''t think C has a stream IO... streaming is kinda a C++ thing.

The fopen/fseek/fclose functions are a bit (ok alot) faster than the fstream class.

I''d expect Windows file IO handled with asynchronous completion ports to yield the best performance, but it''s just a hunch.

It kinda depends on what you''re doing; some things are only possible with event driven asynchrous IO; suppose you want to stream data off disk and do something with it more frequently than 1ms intervals (and not use all available cpu power).

And some things it just doesn''t matter, like read a text file (unless they''re friggin'' huge).

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
i don''t know if they are the fastest, but i love em, ifsteam, and ofstream. i mean you can write whole objects, and structs to bianary files and load them in a few verry easy lines.


  ifstream fin;ofstream fout;bool SaveMap(FileType &SaveingMap, char* Filename){	fout.open(Filename, ios::binary);	if(!fout)	{		MessageBox(NULL, "ERROR SAVING MAP", "Error", MB_OK);		return 0;	}	fout.write((char*) &SaveingMap, sizeof(FileType));	fout.close();	return 1;}/////////////////////////////////////////////////////////////////////bool LoadMap(FileType &LoadingMap, char* Filename){	fin.open(Filename, ios::binary);	if(!fin)	{		MessageBox(NULL, "ERROR Loading MAP", "Error", MB_OK);		return 0;	}	fin.read((char*) &LoadingMap, sizeof(FileType));	fin.close();	return 1;}  


it''s just that easy, if anyone wants to use these functions(though i''m sure most people have very complicated 100x faster versions for them selves but hey, whatever works right) feel free, i''m sure you can see how they work. and i love posting code from my game i''m also on some realy good medicine so i''ll shut up now...


-------------------------------------------------
Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
i think for windows
CreateFile()
WriteFile()
ReadFile()

is the fastest , but its also just a hunch


{ Stating the obvious never helped any situation !! }
Ok .. the answer is already posted .. but i want to add a few tidbits.

There are (at least) 5 different I/O methods ... 3 fast, 2 slow. The exact comparisons I don''t know ... but here are the basics .. from what I assume are slowest to fastest .. but the top 3 are very close and you should run tests to find out which is best.

SLOW - formatted io

1. Hands Down Slowest - formatted I/O in C using fprintf type functions.

2. Second Slowest - formatted I/O in C++ using insertion / extraction operators (<< >>)

FAST

3. C++ file stream''s read and write member functions.
4. C''s binary FILE read / write function ... FREAD i think.
5. Win32s binary file read write functions WriteFile() .. etc

the bottom 3 should be considered equivelent unless you have run tests that say otherwise ... usually you just choose the one that most closely matches the rest of you code ... 3 if you use c++ strstreams or manipulators ... 5 if you are using many other Win32 API functions ... and 4 if you are using C standard library stuff.

I personally use 2 and 3 ... pure c++ all the way ... formatted i/o when usefull and not speed critical ... binary i/o when structured and /or performance critical.

good luck.
Some comments:

1. Asyncronous IO only works on OSes based on NT/2K kernel. So is not supported on Win9x.

2. CreateFile is the fastest... but only if you use the correct flags for your access type, and stick to sensible patterns (FILE_FLAG_SEQUENTIAL_SCAN can help quite a lot if you do access in true sequential order, as can some of the flags).

3. In places where you''re allocating a buffer to read into, doing something with the data and then freeing the buffer - you can actually get even faster still by using Memory Mapped Files... (this is the mechanism Windows uses to handle virtual memory so is very optimal).

In our last game (Pac-Man:Adventures in Time) all data was held in a single monolithic file (.PAC) opened at the start of the game (CreateFile with correct flags). A file mapping object was created for the file, which again was held open throughout the games life. When some data was required from the file, a view was mapped (MapViewOfFile) - this makes the file become part of the process address space, so can be accessed just like real memory - and is buffered and paged into real memory in the same way as virtual memory pages are.

Use CreateFile, CreateFileMapping and MapViewOfFile for the fastest access in Windows...

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement