Guest User

Untitled

a guest
Dec 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. //
  2. // main.c
  3. // arrtest
  4. //
  5. // Created by Mattias (hz) Hansson on 9/2/12.
  6. // Public Domain. Credits/Bugfixes welcome.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <string.h>
  13. #include <sys/time.h>
  14.  
  15. /* Return 1 if the difference is negative, otherwise 0. */
  16. int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
  17. {
  18. long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
  19. result->tv_sec = diff / 1000000;
  20. result->tv_usec = diff % 1000000;
  21.  
  22. return (diff<0);
  23. }
  24.  
  25. unsigned char* buildBitcountLookup(int bits) {
  26. unsigned long BYTE_SIZE = powl(2, bits);
  27. unsigned char* arr = (void*)malloc(BYTE_SIZE);
  28. memset(arr, 0, BYTE_SIZE-1); //Force preallocation of mem
  29.  
  30. unsigned long int i = 0;
  31. for(; i < BYTE_SIZE; i++) {
  32. unsigned char bitCount = 0;
  33. for(unsigned long bitField = i; bitField > 0; bitField = (bitField >> 1)) {
  34. bitCount += (bitField & 1);
  35. }
  36. arr[i] = bitCount;
  37.  
  38. }
  39. return arr;
  40. }
  41.  
  42. unsigned long int* buildRandomArr(int arrLength) {
  43. unsigned long ARR_SIZE = arrLength;
  44. unsigned long BYTE_SIZE = ARR_SIZE * sizeof(unsigned long);
  45. unsigned long* arr = (void*)malloc(BYTE_SIZE);
  46. memset(arr, 0, BYTE_SIZE-1); //Force preallocation of mem
  47. for(int i = 0; i < ARR_SIZE; i++) {
  48. //test
  49. //arr[i] = 0xffffffffffffffffL;
  50. arr[i] = ((((random() & 1) << 31) + random() << 32) | ((random() & 1) << 31) + random());
  51. }
  52. return arr;
  53. }
  54.  
  55. unsigned int bitCountNaive(unsigned long int* testarr, int arrLength) {
  56. unsigned int bitCount = 0;
  57. for(int i = 0; i < arrLength; i++) {
  58. for(unsigned long bitField = testarr[i]; bitField > 0; bitField = (bitField >> 1)) {
  59. bitCount += (bitField & 1);
  60. }
  61. }
  62. return bitCount;
  63. }
  64.  
  65. unsigned int bitCountLookup16(unsigned long int* testarr, int arrLength, unsigned char* lookupArr) {
  66. unsigned int bitCount = 0;
  67. for(int i = 0; i < arrLength; i++) {
  68. unsigned long int val = testarr[i];
  69. bitCount += lookupArr[(val & 0xffff000000000000L) >> 48] +
  70. lookupArr[(val & 0xffff00000000L) >> 32] +
  71. lookupArr[(val & 0xffff0000L) >> 16] +
  72. lookupArr[(val & 0xffffL)];
  73. }
  74. return bitCount;
  75. }
  76.  
  77. int main(int argc, const char * argv[])
  78. {
  79.  
  80. const unsigned long int LOOPS = 10000;
  81. unsigned char* lookupArr = buildBitcountLookup(16);
  82. srandomdev(); //init random
  83. unsigned long* testarr = buildRandomArr(10000);
  84. struct timeval tvBegin, tvEnd, tvDiff;
  85.  
  86. unsigned long int totalBitCount = 0;
  87. //-------------------------------------
  88. printf("Naive approach:\n");
  89. gettimeofday(&tvBegin, NULL);
  90. totalBitCount = 0;
  91. for (int i = 0; i < LOOPS; i++) {
  92. //printf("bitCountNaive = %d\n", bitCountNaive(testarr, 10000));
  93. totalBitCount += bitCountNaive(testarr, 10000);
  94. }
  95. printf("Total %ld bits gives %d avg. bits over %ld iterations.\n", totalBitCount, (int)(totalBitCount/LOOPS), LOOPS);
  96. gettimeofday(&tvEnd, NULL);
  97. timeval_subtract(&tvDiff, &tvEnd, &tvBegin);
  98. printf("%ld.%06d seconds.\n", tvDiff.tv_sec, tvDiff.tv_usec);
  99.  
  100. //-------------------------------------
  101. printf("Lookup 16 bit approach:\n");
  102. gettimeofday(&tvBegin, NULL);
  103. totalBitCount = 0;
  104. for (int i = 0; i < LOOPS; i++) {
  105. //printf("bitCountNaive = %d\n", bitCountNaive(testarr, 10000));
  106. totalBitCount += bitCountLookup16(testarr, 10000, lookupArr);
  107. }
  108. printf("Total %ld bits gives %d avg. bits over %ld iterations.\n", totalBitCount, (int)(totalBitCount/LOOPS), LOOPS);
  109. gettimeofday(&tvEnd, NULL);
  110. timeval_subtract(&tvDiff, &tvEnd, &tvBegin);
  111. printf("%ld.%06d seconds.\n", tvDiff.tv_sec, tvDiff.tv_usec);
  112.  
  113. free(testarr);
  114. free(lookupArr);
  115. printf("done.\n");
  116. return 0;
  117. }
Add Comment
Please, Sign In to add comment