views:

111

answers:

3

I have to read a binary file data into a C# winform application.

The binary reading is frequent, i.e. many form reads different section of data.

I can create a C++ dll to read the binary file and use it in C# application.

or

I can have the reading logic in C#.

The main issue if performance.

If I write it into C# will it give me the lighting speed as of C++.

If I use C++ and every time C# application call it through interfaces will the overall speed will be the same.

A: 

Your bottleneck shouldn't be the code, but the hdd access, so it shouldn't really matter which language you use. Depending on your experience (and the amount of work you want to put into it) it might be entirely possible that doing this in c# is quite a bit faster then doing it completely in c++. C# as a language is not that much slower then c++ (exspecially since it is very easy to shoot yourself in the foot performancewise in c++) and you get the whole .net framework which has performancewise adequate solutions for most things (I would guess that the streamreader classes do a bit more buffering then you would automatically get in c++, which can make quite a difference in performance).

In general this seems like a case of possibly premature optimization, which as we know is evil. Is this part really that performancesensitive? I would guess not (of course, I might be wrong). If it is, will the language make a difference? Probably not (compare the time needed to execute the instructions you write (some arithmetic, some function calls) to read the file (probably measured in nano- or microseconds) with the milliseconds seek time of your typical hdd. If your code can make a difference there, it is most likely only in the access pattern and the amount of buffering. E.g. it might be better to read bigger chunks at once (even if you don't need some of the data) into the memory at once, if this reduces the number of read access (and exspecially seeks). But again write it the obvious way first and only optimize if necessary

Grizzly
+1  A: 

Reading from disk is mostly an I/O bound operation so it will make little difference if you write it in C# or C++.

To maximize performance I would suggest reading the entire file into memory in one go (assuming the file is not too large) rather than seeking backwards and forwards in the file to read different sections.

Mark Byers
I have decided to write the code in C# as there is more risk in C++. We are not using .NET 4 now so can't used MemoryMappedFile class as suggseted by Eugene.I cannot read the file in one go as it will be large, every time i have to go to a particular offset and read 4 attribute value so its fast as I can always calculate where I want to read from.
Mohit
A: 

In C# you may use WinAPI File Management Functions through P/Invoket and this code will be the same (or slightly slower) in meaning of performance.

Also in .NET 4 added MemoryMappedFile class that allows you to map a large files directly into memory and still use safe .NET methods.

And the last word: you may create file mapping in C# by yourself using (once more) P/Invoke.

So the conclusion is: you can use C# for your app without misgivings.

Eugene Cheverda