surprising optimization of comparison operations for __int128_t

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hi,

The attached C++ testcase compares the performance behavior of
__int128_t used directly vs __int128_t used through an overloaded
operator <. The overloaded < operator appears faster than the raw
__int128_t which I find really surprising so, I fear I am not
measuring what I think I am measuring. Hints ?

[mathieu@mathieu-laptop benchmark-time]$ g++ --version
g++ (GCC) 4.4.3 20100127 (Red Hat 4.4.3-4)
[mathieu@mathieu-laptop benchmark-time]$ g++ -O3 test.cc
# run raw __int128_t version
[mathieu@mathieu-laptop benchmark-time]$ time -p ./a.out 100000002 a
16384
2
real 0.60
user 0.60
sys 0.00
# run operator < version
[mathieu@mathieu-laptop benchmark-time]$ time -p ./a.out 100000002 test
16384
2
real 0.40
user 0.40
sys 0.00


Mathieu
-- 
Mathieu Lacage <mathieu.lacage@xxxxxxxxx>
#include <iostream>
#include <stdlib.h>
#include <string.h>

struct Test
{
  inline Test () : v (0) {}
  inline Test (int _v) : v (_v) {}
  __int128_t v;
};
inline bool operator < (const Test &a, const Test &b)
{
  return a.v < b.v;
}

#define CMP_SIZE 1024
template <typename T>
T
run_cmp (T a, T b, long int n)
{
#define SIZE 1024
  bool cmp = false;
  T array[SIZE];
  int count = 0;
  std::cout << sizeof(array) << std::endl;
  for (int i = 0; i < SIZE; i++)
    {
      array[i] = b;
    }
  for (long int i = 0; i < n; i++)
    {
      if (a < array[i%SIZE])
	{
	  count++;
	}
    }
  return count&0x1?a:b;
}


int main (int argc, char *argv[])
{
  long int n = atoi (argv[1]);
  int test = strcmp (argv[2], "test") == 0;
  if (test)
    {
      Test result = run_cmp<struct Test> (Test (1), Test (2), n);
      std::cout << (long int) result.v << std::endl;
    }
  else
    {
      __int128_t result = run_cmp<__int128_t> (1, 2, n);
      std::cout << (long int)result << std::endl;
    }
  return 0;
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux