Working with a C++ data type on a bit by bit level

Started by
28 comments, last by DanBerliner 13 years ago

I've done that, but just to iterate though an 800*600 (480,000) element vector takes my computer about 570-590ms.


That comes out to about 3000 cycles per pixel. Something is not right... It should be at least two orders of magnitude faster. Perhaps you can post some short code that shows the slowness?
Advertisement

Would something like this work?
...

EDIT: You probably want to make the internal array heap allocated if it's going to be large but the idea is the same...


I think this is a bad idea as it just duplicates std::bitset except less efficiently. Also, your implementation has a serious bug since you're rounding down the size of the data array.


Also, isn't std::vector<bool> deprecated? I'd say use std::bitset instead.
I trust exceptions about as far as I can throw them.

Also, isn't std::vector<bool> deprecated? I'd say use std::bitset instead.

I don't think so. There have been proposals to deprecate it, though. The size of a std::bitset has to be known at compile time, which makes it a poor substitute for std::vector<bool>. A more reasonable substitute would be boost::dynamic_bitset.

[quote name='Dan Berliner' timestamp='1302219094' post='4795740']
I've done that, but just to iterate though an 800*600 (480,000) element vector takes my computer about 570-590ms.


That comes out to about 3000 cycles per pixel. Something is not right... It should be at least two orders of magnitude faster. Perhaps you can post some short code that shows the slowness?
[/quote]

I unfortunately don't have the exact code anymore but it was very simple. It was a vector<bool> that was 800*600 in length (it was one dimensional). I then used a simple iterator to go though it in a for loop. Inside the for loop I incremented an int by 1, I didn't even draw a pixel.
In Debug mode or Release mode? Did you try it with _SECURE_SCL disabled on older MSVC versions, as SiCrane mentioned?

[quote name='doesnotcompute' timestamp='1302226064' post='4795774']
Would something like this work?
...

EDIT: You probably want to make the internal array heap allocated if it's going to be large but the idea is the same...


I think this is a bad idea as it just duplicates std::bitset except less efficiently. Also, your implementation has a serious bug since you're rounding down the size of the data array.


Also, isn't std::vector<bool> deprecated? I'd say use std::bitset instead.
[/quote]

I'm not saying my implementation is perfect, I threw it together in 5 minutes, but it's a starting point. I'd say it's probably not less efficient than an STL version if you have to muck around with things like [color=#1C2837][size=2]_SECURE_SCL to get them to not be horribly slow. It sounds like the OP has a very specific use for this and he knows things like the initial array size ahead of time. In my opinion this is a reasonable situation to roll your own solution rather than trying to get some standard library class to work for him.
The code below takes 1.3 seconds total on my not-so-new computer (g++ -O3). That means the operation you were talking about probably takes about 1.3 ms.

#include <iostream>
#include <vector>

int main() {
std::vector<bool> image(800*600, true);

int n=0;

for (int i=0; i<1000; ++i) {
for (int j=0; j<600; ++j) {
for (int i=0; i<800; ++i) {
n += image[800*j+i];
}
}
}
std::cout << n << '\n';
}

@rip-off I was using debug and that is the root of my problem. Doing it off debug improves the code from taking nearly 600ms to a mere 3ms. A 1080p vector only takes 15ms. This project is going to be fun without being able to use debug...

@alvaro, it takes me around 1.2 seconds to do it on Visual Studio off debug and around 3 seconds on GCC, its also a bad idea to use vectors with simple loops; iterators are much more reliable.
Remember that the MSVC debug and release settings aren't magic; they're both just a combination of presets of compiler and linker options. Modern versions of MSVC allow debugging of optimized release builds as well as standard debug builds. If you want you can create a debug mode that disables things like _SECURE_SCL and _HAS_ITERATOR_DEBUGGING so they run faster or a release mode that disables optimizations so it's easier to debug.

Remember that the MSVC debug and release settings aren't magic; they're both just a combination of presets of compiler and linker options. Modern versions of MSVC allow debugging of optimized release builds as well as standard debug builds. If you want you can create a debug mode that disables things like _SECURE_SCL and _HAS_ITERATOR_DEBUGGING so they run faster or a release mode that disables optimizations so it's easier to debug.


Interestingly enough if I hit the "debug now" button while on the release configuration it sees no performance hit at all.

This topic is closed to new replies.

Advertisement