Guest User

Untitled

a guest
Sep 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. // To compile: gcc -std=gnu99 -O2 -o performance performance.c
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <time.h>
  7.  
  8. #define arrcpy(dest,src,size) for(size_t zz=0;zz<size;zz++)dest[zz]=src[zz]
  9. #define dalloc(size) (test_t*)malloc(sizeof(test_t)*size)
  10.  
  11. typedef double test_t;
  12. const size_t SIZE = 1E4;
  13. const size_t ITERATIONS = 1E6;
  14. const int MATH_TEST = 2;
  15. const int MEMORY_TEST = 1;
  16.  
  17. inline void ptrcpy( test_t *src, test_t *dest, size_t size ) {
  18. test_t *srcptr = src+size, *destptr = dest+size;
  19. while ( srcptr > src ) {
  20. *(--destptr) = *(--srcptr);
  21. }
  22. }
  23.  
  24. inline void ptrcpy2( test_t *src, test_t *dest, size_t size ) {
  25. test_t *srcptr = src, *destptr = dest;
  26. test_t *srcend = src+size;
  27. while ( srcptr < srcend ) {
  28. *(destptr++) = *(srcptr++);
  29. }
  30. }
  31.  
  32.  
  33. int main( int argc, char **argv ) {
  34. size_t i, j;
  35. test_t *src = dalloc( SIZE );
  36. test_t *dest = dalloc( SIZE );
  37. test_t *dest2 = dalloc( SIZE );
  38. time_t start_time, run_time;
  39. int tests = MATH_TEST | MEMORY_TEST;
  40.  
  41. if ( argc > 1 && !strcmp( argv[1], "math" )) {
  42. tests = MATH_TEST;
  43. }
  44. if ( argc > 1 && !strcmp( argv[1], "mem" )) {
  45. tests = MEMORY_TEST;
  46. }
  47.  
  48. if ( tests & MEMORY_TEST ) {
  49. printf( "Test data type size: %d bits\n", sizeof( test_t ) << 3 );
  50. printf( "Number of elements: %d\n", SIZE );
  51. printf( "Number of iterations: %d\n", ITERATIONS );
  52.  
  53. printf( "Running memory read/write tests...\n" );
  54. time( &start_time );
  55. for( i=0; i < ITERATIONS; i++ ) {
  56. memcpy( dest, src, sizeof( test_t ) * SIZE );
  57. memcpy( dest2, src, sizeof( test_t ) * SIZE );
  58. }
  59. run_time = time( NULL ) - start_time;
  60. printf( "memcpy() run time %d seconds.\n", (int)run_time );
  61.  
  62. time( &start_time );
  63. for( i=0; i < ITERATIONS; i++ ) {
  64. for( j=0; j < SIZE; j++ ) {
  65. dest[ j ] = src[ j ];
  66. dest2[ j ] = src[ j ];
  67. }
  68. }
  69. run_time = time( NULL ) - start_time;
  70. printf( "for() run time %d seconds.\n", (int)run_time );
  71.  
  72. time( &start_time );
  73. for( i=0; i < ITERATIONS; i++ ) {
  74. for( j=0; j < SIZE; j++ ) {
  75. dest[ j ] = src[ j ];
  76. }
  77. for( j=0; j < SIZE; j++ ) {
  78. dest2[ j ] = src[ j ];
  79. }
  80. }
  81. run_time = time( NULL ) - start_time;
  82. printf( "dual for() run time %d seconds.\n", (int)run_time );
  83.  
  84. // time( &start_time );
  85. // for( i=0; i < ITERATIONS; i++ ) {
  86. // arrcpy( dest, src, SIZE );
  87. // arrcpy( dest2, src, SIZE );
  88. // }
  89. // run_time = time( NULL ) - start_time;
  90. // printf( "arrcpy() run time %d seconds.\n", (int)run_time );
  91.  
  92. time( &start_time );
  93. for( i=0; i < ITERATIONS; i++ ) {
  94. ptrcpy( dest, src, SIZE );
  95. ptrcpy( dest2, src, SIZE );
  96. }
  97. run_time = time( NULL ) - start_time;
  98. printf( "ptrcpy() run time %d seconds.\n", (int)run_time );
  99.  
  100.  
  101. time( &start_time );
  102. for( i=0; i < ITERATIONS; i++ ) {
  103. ptrcpy2( dest, src, SIZE );
  104. ptrcpy2( dest2, src, SIZE );
  105. }
  106. run_time = time( NULL ) - start_time;
  107. printf( "ptrcpy2() run time %d seconds.\n", (int)run_time );
  108.  
  109. }
  110.  
  111. free( src );
  112. free( dest );
  113. free( dest2 );
  114.  
  115. // I'm not sure these are really giving an accurate picture. May need more work.
  116. if ( tests & MATH_TEST ) {
  117. printf( "Running math tests...\n" );
  118. time( &start_time );
  119. for( i=0; i < ITERATIONS * SIZE / 10; i++ ) {
  120. drand48( ) * drand48( );
  121. }
  122. run_time = time( NULL ) - start_time;
  123. printf( "Floating point multiplication test run time %d seconds.\n", (int)run_time );
  124.  
  125. time( &start_time );
  126. for( i=0; i < ITERATIONS * SIZE / 10; i++ ) {
  127. rand( ) * rand( );
  128. }
  129. run_time = time( NULL ) - start_time;
  130. printf( "Integer multiplication test run time %d seconds.\n", (int)run_time );
  131. }
  132.  
  133. return 0;
  134. }
Add Comment
Please, Sign In to add comment