3

I am making making an application in vb.net that will read files using a byte array as buffer, and create parity files for them by xoring the data... What would be the most efficient way of xoring a byte array? I have though of convertinbg the byte array to a bitarray and then run it trough an xor operation and turn it back to a byte array, but that sounds like a very processing expensive task, and i am worried that it might impact read/write speed... is there a better way to do this? thanks...

To avoid confusion: What the application does is read half the file to location 1, the other half to location 2, then a parity (xor of the two parts) to location 3...

2 Answers 2

2

To Xor two byte arrays simply use a for loop and the Xor operator.

VB.Net's Xor will compile to a the CIL Xor opcode which should be subsequently JIT compiled to the very fast x86 XOR processor instruction.

The cost of the Xor operation is likely to be negligible in comparison to the cost of file I/O.

Sign up to request clarification or add additional context in comments.

Comments

0

Depending on what you mean by "efficient" I think you can do better than a simple loop. My initial though is to break the processing into multiple threads. so it will complete faster. You know the size of the input array and therefore the output array so it would be easy to divide load and let each thread fill the appropriate part of the result.

There is a C# article on code project that does similar. Binary operations on byte arrays withparallelism

3 Comments

What i mean by efficient is processing cost that translates to read/write speed. Because this process involves reading and writing files i would like a solution that does not limit the read/write speed of the harddrive by beeing to costly on the cpu. Suppose that i use a loop that xor every byte. The cpu will then have a maximum processing of X bytes /s. The harddrive also has a write/read speed of X bytes /s. if the cpu has a lower X bytes /s capacity than the harddrives read/write, the result would be a slower read/write speed. Efficient then means the code that can xor the most bytes /s...
Another problem is if i have a file whit a size that is not divideable by two, what do i do with the last byte in terms of splitting the file in two...
I agree with @Phillip Trelford that the processor cost will almost certainly "be negligible in comparison to the cost of file I/O." How you decide to handle the odd byte is basically irrelevant to the efficiency. I'd probably pad the "short" half with a 0 byte on load and call it good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.