Following Raindrop7's answer, I decided to base on How-to-store-binary-data-when-you-only-care-about-speed? and do a Time Measurement with this code:
#include <ctime>
#include <ratio>
#include <chrono>
#include <iostream>
#include <vector>
#include <random>
#define T int
std::uniform_int_distribution<int> uni_bit_distribution(0, 1);
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
// g++ -Wall -std=c++0x -O3 toggle_all_bits_one_by_one.cpp
int main()
{
const int N = 1000000;
const int D = 100;
using namespace std::chrono;
high_resolution_clock::time_point t1 = high_resolution_clock::now();
std::vector<T> v;
v.resize(N * D);
for(int i = 0; i < N; ++i)
for(int j = 0; j < D; ++j)
v[j + i * D] = uni_bit_distribution(generator);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
std::cout << "Build " << time_span.count() << " seconds.\n";
t1 = high_resolution_clock::now();
for(int i = 0; i < N; ++i)
for(int j = 0; j < D; ++j)
//v[j + i * D] = v[j + i * D] == 0 ? 1 : 0;
// Build 3.95191 seconds. Time to toggle all bits one by one 0.0490182 seconds.
//v[j + i * D] = !v[j + i * D];
// Build 3.82705 seconds. Time to toggle all bits one by one 0.0477722 seconds.
v[j + i * D] ^= 1;
// Build 3.74881 seconds. Time to toggle all bits one by one 0.046955 seconds.
t2 = high_resolution_clock::now();
time_span = duration_cast<duration<double> >(t2 - t1);
std::cout << "Time to toggle all bits one by one " << time_span.count() << " seconds.\n";
return 0;
}
which on my machine prove that little effect does the method selection gives, as expected. So, in general, focus on readability.
For #define T char
, I got:
v[j + i * D] = v[j + i * D] == 0 ? 1 : 0;
// Build 3.65369 seconds. Time to toggle all bits one by one 0.0580222 seconds.
//v[j + i * D] = !v[j + i * D];
// Build 3.65898 seconds. Time to toggle all bits one by one 0.0573292 seconds.
//v[j + i * D] ^= 1;
// Build 3.95643 seconds. Time to toggle all bits one by one 0.0570291 seconds.
v[2] ^= 1;
instead ofv[3]
– Thriller111
but you may wanted:v[2] ^= 1;
notv[3]
– Thrillervector<char>
. I know what you're doing with them from your previous questions, and there is just no way that following a pointer to a bunch of chars is ever faster than just having the bits right there in an integer. – Spyglassto_ulong
on it, do some operation, then convert back.. – Spyglassstd::vector<char>
is really driven by my timings. Moreover, there is a function to get the results I eventually need based on my previous questions. I would be happy to change if there is reason. – Fungible