SHARE
TWEET

Untitled

a guest Jan 2nd, 2013 786 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Integer and float benchmark for Win32 and Win64
  3. // Results are below main(), line 91
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #ifdef _WIN32
  8. #include <sys/timeb.h>
  9. #else
  10. #include <sys/time.h>
  11. #endif
  12. #include <time.h>
  13.  
  14. double
  15. mygettime(void) {
  16. # ifdef _WIN32
  17.   struct _timeb tb;
  18.   _ftime(&tb);
  19.   return (double)tb.time + (0.001 * (double)tb.millitm);
  20. # else
  21.   struct timeval tv;
  22.   if(gettimeofday(&tv, 0) < 0) {
  23.     perror("oops");
  24.   }
  25.   return (double)tv.tv_sec + (0.000001 * (double)tv.tv_usec);
  26. # endif
  27. }
  28.  
  29. template< typename Type >
  30. void my_test(const char* name) {
  31.   volatile Type v  = 0;
  32.   // Do not use constants or repeating values
  33.   //  to avoid loop unroll optimizations.
  34.   // All values >0 to avoid division by 0
  35.   Type v0 = (Type)(rand() % 256)/16 + 1;
  36.   Type v1 = (Type)(rand() % 256)/16 + 1;
  37.   Type v2 = (Type)(rand() % 256)/16 + 1;
  38.   Type v3 = (Type)(rand() % 256)/16 + 1;
  39.   Type v4 = (Type)(rand() % 256)/16 + 1;
  40.   Type v5 = (Type)(rand() % 256)/16 + 1;
  41.   Type v6 = (Type)(rand() % 256)/16 + 1;
  42.   Type v7 = (Type)(rand() % 256)/16 + 1;
  43.  
  44.   double t1 = mygettime();
  45.   for (size_t i = 0; i < 100000000; ++i) {
  46.     v += v0;
  47.     v += v2;
  48.     v += v4;
  49.     v += v6;
  50.   }
  51.   printf("%s add: %f\n", name, mygettime() - t1);
  52.  
  53.   t1 = mygettime();
  54.   for (size_t i = 0; i < 100000000; ++i) {
  55.     v -= v1;
  56.     v -= v3;
  57.     v -= v5;
  58.     v -= v7;
  59.   }
  60.   printf("%s sub: %f\n", name, mygettime() - t1);
  61.  
  62.   t1 = mygettime();
  63.   for (size_t i = 0; i < 100000000; ++i) {
  64.     v *= v0;
  65.     v *= v2;
  66.     v *= v4;
  67.     v *= v6;
  68.   }
  69.   printf("%s mul: %f\n", name, mygettime() - t1);
  70.  
  71.   t1 = mygettime();
  72.   for (size_t i = 0; i < 100000000; ++i) {
  73.     v /= v1;
  74.     v /= v3;
  75.     v /= v5;
  76.     v /= v7;
  77.   }
  78.   printf("%s div: %f\n", name, mygettime() - t1);
  79. }
  80.  
  81. int main() {
  82.   my_test<     short >("    short");
  83.   my_test<      long >("     long");
  84.   my_test< long long >("long long");
  85.   my_test<     float >("    float");
  86.   my_test<    double >("   double");
  87.  
  88.   return 0;
  89. }
  90.  
  91. ///////////////////////////////////////////////////////
  92.  
  93. Win32:
  94.     short add:  0.889000
  95.     short sub:  0.892000
  96.     short mul:  1.182000
  97.     short div:  3.352000
  98.      long add:  0.885000
  99.      long sub:  0.884000
  100.      long mul:  1.178000
  101.      long div:  3.381000
  102. long long add:  1.152000
  103. long long sub:  1.135000
  104. long long mul:  1.971000
  105. long long div:  4.484000
  106.     float add:  1.423000
  107.     float sub:  1.432000
  108.     float mul: 50.639000
  109.     float div: 52.085000
  110.    double add:  1.425000
  111.    double sub:  1.425000
  112.    double mul: 50.774000
  113.    double div: 51.748000
  114.  
  115. Win64:
  116.     short add:  1.204000
  117.     short sub:  1.249000
  118.     short mul:  1.437000
  119.     short div:  3.590000
  120.      long add:  0.856000
  121.      long sub:  0.855000
  122.      long mul:  1.146000
  123.      long div:  3.291000
  124. long long add:  0.876000
  125. long long sub:  0.861000
  126. long long mul:  1.145000
  127. long long div:  6.035000
  128.     float add:  1.422000
  129.     float sub:  1.419000
  130.     float mul:  1.555000
  131.     float div:  1.997000
  132.    double add:  1.421000
  133.    double sub:  1.415000
  134.    double mul:  1.714000
  135.    double div:  2.002000
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top