Lately I\'ve been asked to write a function that reads the binary file into the std::vector
where BYTE
is an unsigned char
I would have thought that the first method, using the size and using stream::read()
would be the most efficient. The "cost" of casting to char *
is most likely zero - casts of this kind simply tell the compiler that "Hey, I know you think this is a different type, but I really want this type here...", and does not add any extra instrucitons - if you wish to confirm this, try reading the file into a char array, and compare the actual assembler code. Aside from a little bit of extra work to figure out the address of the buffer inside the vector, there shouldn't be any difference.
As always, the only way to tell for sure IN YOUR CASE what is the most efficient is to measure it. "Asking on the internet" is not proof.