io speed c# c++

Hi,
Could you please tell me which is the fastest method of io? I am currently using a binary writer in c#. Is it worth using streams such as fstream in mc++ or c++, using interoperability.
 I am looking for performance mainly for int's and float's. For example to write and read an array of 1million floats, I need the fastest method.
thanks in advance
dm14011Asked:
Who is Participating?
 
rcarlanCommented:
You should look into memory mapped files. They are the fastest means of file I/O in Windows (as far as I know).
With memory mapped files, you basically read/write from memory and leave the flushing to/reading from file to the virtual memory manager. Flushing to file is done by background threads - the ones used by the virtual memory manager for paging.
Memory mapped files are particularly good when you need random access in large files.

Radu
0
 
melodiesoflifeCommented:
If you prefer about performance, don't use C#. C++ is the best choice.
0
 
NetworkArchitekCommented:
C++ is certainly the fastest, it is completely unmanaged. But this is theoretically true, in many applications you won't see a benefit unless you are writing a *lot* of code that gets right to the mettle. I would like to see someone take a 1ghz machine and write this application in c++ and c# and benchmark it. My guess is the performance difference would be negiligible. I was just commenting on the idea of c++ vs c#, in practice Rcarlan has the best solution.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
rcarlanCommented:
One thing that I've noticed is very expensive in C# (at least in VS.NET 2003) is throwing and catching exceptions. And I know this also applies to Java.

Exceptions are expensive in any language (which is why some prefer to use SEH rather than C++ exceptions), but, based on the tests I've done, C# seems an order of magnitude slower than C++. I assume it's due to the managed environment - i.e. managed C++, VB.NET, J# would all suffer in the same way.

Radu
0
 
dm14011Author Commented:
Hi again. thanks for your comments. This is all very interesting. Just to expand a bit, I have an io intensive application that aims to potentially access dynamically up to several billion values in its lifetime. (the values must remain in case they are reaccessed. They are also changeable by the user and hence must be saved and reloaded and not just be stored by a function or reallocatable). I much prefer to use c# because of its production rate and ease of use. Ill have a look at the memory mapped files. That sounds good - thanks. As NetwrokArchitek mentioned I'm not sure how c# and c++ compare. I might do a little benchmarking myself and see what happens. I also currently have interop costs to consider, which may slow things. Maybe Ill get back with some of the results of my tests. Until then if anyone has any more ideas of comments then please post . Also is ifstream and ofstream the fastest i/o in c++, oris there anything in c, or any other classes/functions which are faster/more efficient?
cheers
0
 
rcarlanCommented:
I don't think ifstream and ofstream will beat memory mapped files.

The interop is costly. If your native app is C#, stick with C#. You will lose more by doing interopo between managed and unmanaged code, than you'd gain from coding the I/O in C++.

Radu
0
 
NetworkArchitekCommented:
Yeah, memory mapped files are the way to go. Actually, this isn't necessarily an either or situation. I haven't done this with C++ but I have with C# and VB, and I know you can do it in VS 03 but it works even better in '05, you can mix projects. You can write the bulk of the app in C# and then write the i/o intensive stuff in C++. Its completely seemless with C# and VB so you may want to look on MSDN for how to do it successfully with C++, as I haven't tried it. This might be a good compromise. And just to reinforce what Rcarlan said, Interop is very expensive, I only use it if there is no other way to do it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.