Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

File I/O

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 level10boy   Members   


Posted 18 June 2001 - 01:31 PM

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

#2 Shannon Barber   Moderators   


Posted 18 June 2001 - 02:07 PM

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

#3 Authustian   Members   


Posted 18 June 2001 - 03:05 PM

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);
MessageBox(NULL, "ERROR SAVING MAP", "Error", MB_OK);
return 0;

fout.write((char*) &SaveingMap, sizeof(FileType));
return 1;


bool LoadMap(FileType &LoadingMap, char* Filename)
fin.open(Filename, ios::binary);
MessageBox(NULL, "ERROR Loading MAP", "Error", MB_OK);
return 0;

fin.read((char*) &LoadingMap, sizeof(FileType));
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

#4 jwalker   Members   


Posted 18 June 2001 - 03:09 PM

i think for windows

is the fastest , but its also just a hunch

{ Stating the obvious never helped any situation !! }

#5 Xai   Members   


Posted 18 June 2001 - 03:26 PM

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 (<< >>)


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.

#6 S1CA   Members   


Posted 18 June 2001 - 11:48 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.