create file in RAM memory?

K

kathy

Read / Write to disk file is too slow. How to create a file in RAM
memory so that I could read / write to memory?
 
R

red floyd

Read / Write to disk file is too slow. How to create a file in RAM
memory so that I could read / write to memory?

Did you have a C++ language question?
 
A

Alan Woodland

red said:
Did you have a C++ language question?
If you squint a bit the question is 'how do I make an fstream read/write
from/to memory?'

In which case the answer would be stringstream I guess...

Alan
 
J

Jerry Coffin

Read / Write to disk file is too slow. How to create a file in RAM
memory so that I could read / write to memory?

It's not really a "file in RAM", but depending on what you want, a
stringstream might work.
 
T

Thomas Matthews

kathy said:
Read / Write to disk file is too slow. How to create a file in RAM
memory so that I could read / write to memory?

Review how you are reading from and writing to the disk file.
Disk drives love large chunks of data, the larger the better.

A disk drive requires some time to ramp up to speed, then
time to continue access, then time to ramp down. When large
chunks of data are accessed, the ramp up and ramp down times
are not a large chunk of the time.

When I optimize File I/O, I usually create a huge buffer in
memory (array of 5 MB of unsigned char or larger), then read
in all the data using a single fread() call. If the file is
larger than my buffer size, I just do more loads.

I reduced a program's execution speed, to parse a 1GB file,
from 45 minutes to 4 minutes by using the above technique.
Another technique is loop unrolling.

You may also want to check if your platform has an API for
reading in entire files into memory or using memory as a file.
This is platform specific and not discussed here.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
M

Michael Doubez

Read / Write to disk file is too slow. How to create a file in RAM
memory so that I could read / write to memory?

If you are expecting a FILE* to map to memory, there is no stand
 
M

Michael Doubez

-- Wrong send, sorry for the noise --

If you are expecting a FILE* to map to memory, there is no standard
way to do it. On some system (like Linux), there are ways to map a
path to a filesystem in memory (tmpfs), there are also libs to fake
it.

If you just want a stream to memory, you can use rdbuf() method with a
stringbuf to write in a string or with your own streambuf (using non-
contiguous chunks of memory).
 
J

Juha Nieminen

Michael said:
If you just want a stream to memory, you can use rdbuf() method with a
stringbuf to write in a string or with your own streambuf (using non-
contiguous chunks of memory).

How would that be different from using a stringstream?
 
B

Balog Pal

kathy said:
Read / Write to disk file is too slow. How to create a file in RAM
memory so that I could read / write to memory?

The MFC library has a class CMemoryFile ( CMemFile ?) that has the same
interface as CFile (so is substitutable), but works on a memory block.

Probably other file wrapper librarise have a similar class, but writing one
is not a big deal either.
 
M

Michael Doubez

  How would that be different from using a stringstream?

stringstream doesn't guarantee chunks are used. If the underlying data
is a string (as is the case in gcc 3.4.3 by example) it can be costly
for large data.
 
J

Jorgen Grahn

Read / Write to disk file is too slow. How to create a file in RAM
memory so that I could read / write to memory?

You should probably state your /actual/ problem to get useful replies.
On modern OSes, "disk" file operations work on RAM buffers which are
filled or periodically flushed to disk ... so for many uses disk
access isn't really the limiting factor. Unless you are trying to
dump gigabit network traffic to disk or something. Look at your
hardware and do the math -- it is likely that sustained I/O speed is
not the limiting factor.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top