test-bitvector.nut 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. auto bv_size = 800000;
  2. auto step = 1;
  3. const BITVEC_SZ = 8;
  4. auto sqBitVect = blob((bv_size / BITVEC_SZ) + 512);
  5. //auto sqBitVect = array_int8((bv_size / BITVEC_SZ) + 512);
  6. //sqBitVect.memset(0, 0, sqBitVect.len()-1);
  7. int_t bitGet(size_t bpos)
  8. {
  9. auto cpos = bpos/BITVEC_SZ;
  10. auto b8 = 1 << (bpos%BITVEC_SZ);
  11. return sqBitVect[cpos] & b8;
  12. }
  13. void bitSet(size_t bpos)
  14. {
  15. auto cpos = bpos/BITVEC_SZ;
  16. auto b8 = 1 << (bpos%BITVEC_SZ);
  17. sqBitVect[cpos] |= b8;
  18. }
  19. void bitClear(size_t bpos)
  20. {
  21. auto cpos = bpos/BITVEC_SZ;
  22. auto b8 = 1 << (bpos%BITVEC_SZ);
  23. sqBitVect[cpos] &= (~b8);
  24. }
  25. void bitTogle(size_t bpos)
  26. {
  27. auto cpos = bpos/BITVEC_SZ;
  28. auto b8 = 1 << (bpos%BITVEC_SZ);
  29. sqBitVect[cpos] ^= b8;
  30. }
  31. auto bit_pos = 3;
  32. auto cpos = bit_pos/BITVEC_SZ;
  33. auto m8 = (bit_pos%BITVEC_SZ);
  34. auto b8 = 1 << m8;
  35. print(bit_pos, cpos, m8, b8);
  36. print(bitGet(bit_pos));
  37. bitSet(bit_pos);
  38. print(bitGet(bit_pos));
  39. bitClear(bit_pos);
  40. print(bitGet(bit_pos));
  41. bitTogle(bit_pos);
  42. print(bitGet(bit_pos));
  43. print("===");
  44. ++bit_pos;
  45. if(type(sqBitVect) != "array")
  46. {
  47. print(sqBitVect.bitGet(bit_pos));
  48. sqBitVect.bitSet(bit_pos);
  49. print(sqBitVect.bitGet(bit_pos));
  50. sqBitVect.bitClear(bit_pos);
  51. print(sqBitVect.bitGet(bit_pos));
  52. sqBitVect.bitTogle(bit_pos);
  53. print(sqBitVect.bitGet(bit_pos));
  54. }
  55. //return;
  56. //BitVector is 1 based
  57. auto bv = BitVector(bv_size);
  58. print(bv);
  59. auto start_milli = os.getmillicount();
  60. for(auto i=1; i <= bv_size; i+=step) bv.set(i);
  61. print("Time spent set bv a", os.getmillicount() - start_milli);
  62. start_milli = os.getmillicount();
  63. for(auto i=1; i <= bv_size; i+=step) bv[i] = false;
  64. print("Time spent set bv b", os.getmillicount() - start_milli);
  65. start_milli = os.getmillicount();
  66. for(auto i=1; i <= bv_size; i+=step) bv[i] = true;
  67. print("Time spent set bv c", os.getmillicount() - start_milli);
  68. start_milli = os.getmillicount();
  69. for(auto i=1; i <= bv_size; i+=step) bv.test(i);
  70. print("Time spent get bv a", os.getmillicount() - start_milli);
  71. start_milli = os.getmillicount();
  72. for(auto i=1; i <= bv_size; i+=step) bv[i];
  73. print("Time spent get bv b", os.getmillicount() - start_milli);
  74. start_milli = os.getmillicount();
  75. for(auto i=0; i < bv_size; i+=step) bitSet(i);
  76. print("Time spent set sq", os.getmillicount() - start_milli);
  77. start_milli = os.getmillicount();
  78. for(auto i=0; i < bv_size; i+=step) bitGet(i);
  79. print("Time spent get sq", os.getmillicount() - start_milli);
  80. auto cbitSet, cbitGet;
  81. if(type(sqBitVect) != "array")
  82. {
  83. start_milli = os.getmillicount();
  84. for(auto i=0; i < bv_size; i+=step) sqBitVect.bitSet(i);
  85. print("Time spent set blob", os.getmillicount() - start_milli);
  86. start_milli = os.getmillicount();
  87. for(auto i=0; i < bv_size; i+=step) sqBitVect.bitGet(i);
  88. print("Time spent get blob", os.getmillicount() - start_milli);
  89. cbitSet = sqBitVect.bitSet;
  90. cbitGet = sqBitVect.bitGet;
  91. start_milli = os.getmillicount();
  92. for(auto i=0; i < bv_size; i+=step) rawcall(cbitSet, sqBitVect, i);
  93. print("Time spent set rawcall blob", os.getmillicount() - start_milli);
  94. start_milli = os.getmillicount();
  95. for(auto i=0; i < bv_size; i+=step) rawcall(cbitGet, sqBitVect, i);
  96. print("Time spent get rawcall blob", os.getmillicount() - start_milli);
  97. }
  98. cbitSet = BitVector.set;
  99. cbitGet = BitVector.test;
  100. print(cbitSet, cbitGet);
  101. //bv = BitVector(bv_size);
  102. start_milli = os.getmillicount();
  103. for(auto i=1; i <= bv_size; i+=step) rawcall(cbitSet, bv, i);
  104. print("Time spent set rawcall bv", os.getmillicount() - start_milli);
  105. start_milli = os.getmillicount();
  106. for(auto i=1; i <= bv_size; i+=step) rawcall(cbitGet, bv, i);
  107. print("Time spent get rawcall bv", os.getmillicount() - start_milli);
  108. //bv = array(bv_size);
  109. bv = array_int8(bv_size);
  110. start_milli = os.getmillicount();
  111. for(auto i=0; i < bv_size; i+=step) bv[i] = 1;
  112. print("Time spent set array", os.getmillicount() - start_milli);
  113. start_milli = os.getmillicount();
  114. for(auto i=0; i < bv_size; i+=step) bv[i];
  115. print("Time spent get array", os.getmillicount() - start_milli);