Guest User

Untitled

a guest
May 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include <mach/mach_time.h>
  2. #include <time.h>
  3. #include <stdio.h>
  4.  
  5. #define DUFF_DEVICE_8(aCount, aAction) \
  6. do { \
  7. int count_ = (aCount); \
  8. int times_ = (count_ + 7) >> 3; \
  9. switch (count_ & 7){ \
  10. case 0: do { aAction; \
  11. case 7: aAction; \
  12. case 6: aAction; \
  13. case 5: aAction; \
  14. case 4: aAction; \
  15. case 3: aAction; \
  16. case 2: aAction; \
  17. case 1: aAction; \
  18. } while (--times_ > 0); \
  19. } \
  20. } while (0)
  21.  
  22. void mach_absolute_difference(uint64_t end, uint64_t start, struct timespec *tp) {
  23. uint64_t difference = end - start;
  24. static mach_timebase_info_data_t info = {0,0};
  25.  
  26. if (info.denom == 0)
  27. mach_timebase_info(&info);
  28.  
  29. uint64_t elapsednano = difference * (info.numer / info.denom);
  30.  
  31. tp->tv_sec = elapsednano * 1e-9;
  32. tp->tv_nsec = elapsednano - (tp->tv_sec * 1e9);
  33. }
  34.  
  35. void *simplememset(void *_p, unsigned v, unsigned count)
  36. {
  37. unsigned char *p = _p;
  38. while(count-- > 0) *p++ = v;
  39. return _p;
  40. }
  41.  
  42. void *duffmemset(void *_p, unsigned v, unsigned count)
  43. {
  44. register n = (count + 7) / 8; /* count > 0 assumed */
  45. unsigned char *p = _p;
  46.  
  47. switch (count % 8)
  48. {
  49. case 0: do {*p++ = v;
  50. case 7: *p++ = v;
  51. case 6: *p++ = v;
  52. case 5: *p++ = v;
  53. case 4: *p++ = v;
  54. case 3: *p++ = v;
  55. case 2: *p++ = v;
  56. case 1: *p++ = v;
  57. } while (--n > 0);
  58. }
  59.  
  60. return _p;
  61. }
  62.  
  63. int main(int argc, char *argv[])
  64. {
  65. uint64_t start,end;
  66. struct timespec tp;
  67. int rep;
  68. int i;
  69. int arraylen = atoi(argv[1]);
  70. unsigned *array, *tmp;
  71. printf("Array Length: %d\n", arraylen);
  72.  
  73. for( rep = 0; rep < 5; rep++ )
  74. {
  75. array = ( unsigned * )malloc( arraylen * sizeof( unsigned ) );
  76. start = mach_absolute_time();
  77. memset(array, 0, arraylen * sizeof( unsigned ));
  78. end = mach_absolute_time();
  79. mach_absolute_difference(end, start, &tp);
  80. if( rep > 0 )
  81. printf("Native: %lu seconds, %lu nanoseconds\n", tp.tv_sec, tp.tv_nsec);
  82. free( array );
  83.  
  84. array = ( unsigned * )malloc( arraylen * sizeof( unsigned ) );
  85. start = mach_absolute_time();
  86. duffmemset(array, 0, arraylen);
  87. //tmp = array;
  88. //DUFF_DEVICE_8( arraylen, *tmp++ = 0 );
  89. end = mach_absolute_time();
  90. mach_absolute_difference(end, start, &tp);
  91. if( rep > 0 )
  92. printf("Duff: %lu seconds, %lu nanoseconds\n", tp.tv_sec, tp.tv_nsec);
  93. free( array );
  94.  
  95. array = ( unsigned * )malloc( arraylen * sizeof( unsigned ) );
  96. start = mach_absolute_time();
  97. simplememset(array, 4, arraylen * sizeof( unsigned ) );
  98. end = mach_absolute_time();
  99. mach_absolute_difference(end, start, &tp);
  100. if( rep > 0 )
  101. printf("Simple: %lu seconds, %lu nanoseconds\n", tp.tv_sec, tp.tv_nsec);
  102. free( array );
  103.  
  104. printf("\n");
  105. }
  106.  
  107. return 0;
  108. }
Add Comment
Please, Sign In to add comment