Question

I want to know that is there any difference between QByteArray and unsigned char* in speed? I have lots of byte arrays and copy operations in my program, so it's important for me to know if there's any difference or not. Thanks

Was it helpful?

Solution

unsigned char* used as a byte array is as simple as it gets. So I would say that in theory QByteArray cannot be faster than that, although it can be as fast or only a little bit slower. But you cannot (usually) use unsigned char* alone. You have to keep the array length somewhere, so it becomes unsigned char* and int, which at least for me is much more work than just using QByteArray. QByteArray also contains a lot of member functions. If you need to implement many of those by yourself when using unsigned char*, there is always the possibility that your implementation is slower than the Qt's implementation. And it takes a lot of work.

Of course, in some applications you may have millions of arrays which are all the same size and you don't need the size variable for all arrays. Then using unsigned char* would be a better idea, especially if the arrays are short and therefore the memory usage overhead of QByteArray would be significant.

I would say that the unsigned char* is faster. But depending on your application and coding skills, it can also be only as fast or slower than QByteArray. If you really want to know, create a test application and measure the speeds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top