造轮子写bitset【STL的bitset很好了…… 自己造的轮子虽然更快,但是不实在】

结论:

ACM比赛要bitset还是直接用STL




发现: 左移1e7次,STL的bitset要用0.7~0.8秒左右!

而一个long long / int 只要0.06秒左右


于是准备自己写一个bitset


然后……同样的操作,在inline后,0.48秒上下浮动。可以比STL快一倍,但是依然常数巨大。


只造了一小部分做实验,希望有菊苣能解决这个bitset低效率的问题,希望流传出高效率的bitset板子。


代码:只做了左移部分


#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#include <tr1/unordered_map>
#include <ctime>
using namespace std::tr1;
using std::sort;
using std::bitset;
using std::max;
using std::cout;
using std::stack;
using std::cin;
using std::endl;
using std::swap;
using std::pair;
using std::vector;
using std::set;
using std::map;
using std::multiset;
using std::queue;
using std::greater;
using std::string;
using std::priority_queue;
using std::max_element;
using std::min_element;
using __gnu_pbds::pairing_heap_tag;
__gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> heap;
#define Hash unordered_map
#define pr(x) cout<<#x<<" = "<<x<<" "
#define prln(x)    cout<<#x<<" = "<<x<<endl


#define  LLL  64
typedef unsigned long long ULL;
int ref_size(int set_size)
{
	return (int) floor(1.0 * set_size / LLL);
}


template<int set_size>
struct Bit_set
{
	ULL  a[(int) floor(1.0 * set_size / LLL)];//lll is the 'long long length'
	Bit_set()
	{
		memset(a, 0, sizeof(a));
	}
	bool operator [] (int pos)	const	//作为右值
	{
		return a[pos / LLL] & (1 << (pos % LLL));
	}

	#define bit_first_idx(k)  (k/LLL)
	#define bit_second_idx(k) (k%LLL)
	//第k位,首先位于first_idx的数字上,然后位于这个数字的second_idx位上
	//下标从0开始

	bool get_bit(int pos)	const	//得到第wz的01
	{
		return a[pos / LLL]  & (1  << (pos % LLL) );
	}
	void set_bit(int pos, bool value)	  //第pos位的01是设置成
	{
		if (!value)	a[pos / LLL] &= (1 << (pos % LLL)) ;
		else a[pos / LLL] |= (1 << (pos % LLL)) ;
	}
	//Bit_set operator << ()
};

template<int set_size>
inline Bit_set<set_size> operator << (Bit_set<set_size> arg, int pos)
{
	int tot_size = ref_size(set_size);
	for (int i = tot_size; i ; -- i)
	{
		arg.a[i] <<= pos;
		arg.a[i] |= (arg.a[i - 1] >> (LLL - pos) );
	}
	arg.a[0] <<= pos;
	return arg;
}

Bit_set<64>q;
bitset<64>sb;

int main()
{
	/*
	q.a[0]=4;
	cout<<q.a[0]<<endl;
	for (int i = 0; i <= 10;++i)
		q.set_bit(i, 1);
	for (int i = 0; i <= 10; ++i)
		cout << q.get_bit(i)<< " ";cout<<endl;
	q = q << 4;
	for (int i = 0; i <= 10; ++ i)
	cout << q[i]<<" ";	cout<<endl;
	*/
	double bg= clock();
	//for (int i = 1;i<=1e7;++i)
	//	q<<1;
	int p;
	for (int i = 1; i<=1e7;++i)
		q<<1;
	cout<< (clock()-bg)/1000<<endl;

	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值