binary_cmov is slower than binary?
Opened this issue · 0 comments
ltang666 commented
I tested it found that for 128 size array binary_cmov is slower than binary on my x86_64, 8 CPU cores, and CPU MHz: 2194.916 machine, which showed binary_cmov was slower than binary.
compile: g++ -o b binary_search.cc --std=c++11 -O3
output:
time binary_cmov : 1.46695
time binary: 1.01361
my test program:
#include <iostream>
#include <chrono>
#include <ctime>
static int
binary_cmov (const int *arr, int n, int key) {
int min = 0, max = n;
while (min < max) {
int middle = (min + max) >> 1;
asm ("cmpl %3, %2\n\tcmovg %4, %0\n\tcmovle %5, %1"
: "+r" (min),
"+r" (max)
: "r" (key), "g" (arr [middle]),
"g" (middle + 1), "g" (middle));
}
return min;
}
static int
binary (const int *arr, int n, int key) {
int min = 0, max = n;
while (min < max) {
int middle = (min + max) >> 1;
if (key > arr [middle])
min = middle + 1;
else
max = middle;
}
return min;
}
int main() {
int size = 128;
int arr[size];
for (int i = 0; i < 128; ++i) {
arr[i] = i;
}
const int loop = 100000;
auto t_start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < loop; i++) {
binary_cmov(arr, size, 40);
}
auto t_end = std::chrono::high_resolution_clock::now();
std::cout << "time binary_cmov: " << std::chrono::duration<double, std::milli>(t_end-t_start).count() << '\n';
auto t_start1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < loop; i++) {
binary(arr, size, 40);
}
auto t_end1 = std::chrono::high_resolution_clock::now();
std::cout << "time binary: " << std::chrono::duration<double, std::milli>(t_end1-t_start1).count() << '\n';
}