测量c ++中popcount函数的时间

时间:2011-11-14 11:25:22

标签: c++ algorithm bit cpu-speed

我感兴趣的是如何把它放在循环中以便获得cpu执行每个不同操作的实时时间

#include<iostream>
#include<cstdlib>
#include<time.h>

using namespace std;
typedef unsigned __int64 uint64;
const uint64 m1=0x5555555555555555;
const uint64 m2=0x3333333333333333;
const uint64 m4=0x0f0f0f0f0f0f0f0f;
const uint64 m8=0x00ff00ff00ff00ff;
const uint64 m16=0x0000ffff0000ffff;
const uint64 m32=0x00000000ffffffff;
const uint64 hff=0xffffffffffffffff;
const uint64 h01=0x0101010101010101;

uint64 popcount_1(uint64 x)
{
    x=(x&m1)+((x>>1)&m1);
    x=(x&m2)+((x>>2)&m2);
    x=(x&m4)+((x>>4)&m4);
    x=(x&m8)+((x>>8)&m8);
    x=(x&m16)+((x>>16)&m16);
    x=(x&m32)+((x>>32)&m32);
    return (uint64)x;
}

//This uses fewer arithmetic operations than any other known
//implementation on machines with slow multiplication.
//It uses 17 arithmetic operations.
int popcount_2(uint64 x)
{
    x-=(x>>1)&m1;//put count of each 2 bits into those 2 bits
    x=(x&m2)+((x>>2)&m2);//put count of each 4 bits into those 4 bits
    x=(x+(x>>4))&m4; //put count of each 8 bits into those 8 bits
    x+=x>>8;//put count of each 16 bits into their lowest 8 bits
    x+=x>>16;
    x+=x>>32;
    return x&0x7f;
}
////This uses fewer arithmetic operations than any other known
//implementation on machines with fast multiplication.
//It uses 12 arithmetic operations, one of which is a multiply.
int popcount_3(uint64 x)
{
    x-=(x>>1)&m1;
    x=(x&m2)+((x>>2)&m2);
    x=(x+(x>>4))&m4;
    return (x*h01)>>56;
}
uint64 popcount_4(uint64 x)
{
    uint64  count;
    for(count=0; x; count++)
    {
        x&=x-1;
    }
    return count;
}
uint64 random()
{
    uint64 r30=RAND_MAX*rand()+rand();
    uint64 s30=RAND_MAX*rand()+rand();
    uint64  t4=rand()&0xf;
    uint64 res=(r30<<34 )+(s30<<4)+t4;
    return res;
}
int main()
{
    int testnum;
    while (true)
    {
        cout<<"enter number of test "<<endl;
        cin>>testnum;
        uint64 x= random();
        switch(testnum)
        {
            case 1: {
                    clock_t start=clock();
                    popcount_1(x);
                    clock_t end=clock();
                    cout<<"execution time of first method"<<start-end<<" "<<endl;
                }
                break;
            case 2: {
                    clock_t start=clock();
                    popcount_2(x);
                    clock_t end=clock();
                    cout<<"execution time of first method"<<start-end<<" "<<endl;
                }
                break;
            case 3: {
                    clock_t start=clock();
                    popcount_3(x);
                    clock_t end=clock();
                    cout<<"execution time of first method"<<start-end<<" "<<endl;
                }
                break;
            case 4: {
                    clock_t start=clock();
                    popcount_4(x);
                    clock_t end=clock();
                    cout<<"execution time of first method"<<start-end<<" "<<endl;
                }
                break;
            default:
                cout<<"it is not correct number "<<endl;
                break;
        }
    }
    return 0;
}
尽管我输入了哪个数字测试,它在终端写入总是零,但很明显为什么因为10或甚至20和100操作对于现代计算机来说都不是什么,但我怎么能点到这样得到如果不是确切的答案,至少近似?非常感谢

2 个答案:

答案 0 :(得分:5)

只需重复所有测试。

以下每次测试重复 1 Mio(1024 * 1024) 2 ^ 25次。您可能希望将时间除以得到的时间以纳秒为单位,但为了进行比较,总数可以很好(并且更容易阅读)。

int main()
{
    int testnum;
    while (true)
    {
        cout<<"enter number of test "<<endl;
        cin>>testnum;
        uint64 x= random();

        clock_t start=clock();
        switch(testnum)
        {
            case 1: for(unsigned long it=0; it<=(1ul<<26); ++it) popcount_1(x); break;
            case 2: for(unsigned long it=0; it<=(1ul<<26); ++it) popcount_2(x); break;
            case 3: for(unsigned long it=0; it<=(1ul<<26); ++it) popcount_3(x); break;
            case 4: for(unsigned long it=0; it<=(1ul<<26); ++it) popcount_4(x); break;
            default:
                cout<<"it is not correct number "<<endl;
                break;
        }
        clock_t end=clock();
        cout<<"execution time of method " << testnum << ": " << (end-start) <<" "<<endl;
    }
    return 0;
}

注释也已修复 start-end(end-start):)

答案 1 :(得分:3)

您想要执行非常便宜的操作的微基准测试。你需要:

  • 围绕便宜的操作制定循环;一个需要足够长时间才能合理的时间;例如大约一秒钟。
  • 确保在下一个中使用一次循环迭代的结果,以避免编译器完全省略正文。
  • 将整个循环包含在一个函数中,使用特定于编译器的属性将该函数标记为not-inlinable (同样,确保编译器不会忽略该调用)并调用功能来自您的计时功能。 或者,根据所有循环迭代返回一个值,并在主程序中实际使用该返回值(例如将其打印或存储在volatile变量中)确保编译器不仅可以优化程序并将其删除。
  • 此外,您应使用高分辨率计时器而不是clock()。在Windows上,这将是QueryPerformanceCounter(&tick_count),在unix clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &timespec_var)上,并且在macos上查看mach_absolute_time()。 (某些)这些方法的另一个优点是它们可以测量CPU时间,而不是挂钟时间,因此在面对系统上的其他活动时,它的变化稍微小一些。

再次,绝对关键确保您实际使用计算的值,方法是将它们存储在volatile变量中,打印它们或返回它们来自非内联函数,以确保编译器不能只是优化它们。并且你想要将你的核心方法标记为不可嵌入,因为函数调用开销很可能会破坏这样的微基准测试;出于类似的原因,您应该避免使用random。这就是为什么你应该对包含一个调用你真正感兴趣的(inlinable)函数的循环的函数进行基准测试。

例如:

#include <iostream>
#include <time.h>
typedef unsigned __int64 uint64;

inline uint64 popcount_1(uint64 x)// etc...

template<typename TF>
uint64 bench_intfunc_helper(TF functor, size_t runs){//benchmark this
    uint64 retval = 0;
    for(size_t i=0; i<runs; ++i) retval += functor(i); 
    // note that i may not have a representative distribution like this
    return retval;//depends on all loop iterations!
}
template<typename TF>
double bench_intfunc(TF functor, size_t runs){
    clock_t start=clock();//hi-res timers would be better
    volatile auto force_evalution = bench_intfunc_helper(functor,runs);
    clock_t end=clock();
    return (end-start)/1000.0;
}
#define BENCH(f) do {std::cout<<"Elapsed time for "<< RUNS <<" runs of " #f \
    ": " << bench_intfunc([](uint64 x) {return f(x);},RUNS) <<"s\n"; } while(0)

int main() {
    BENCH(popcount_1);
    BENCH(popcount_2);
    BENCH(popcount_3);
    BENCH(popcount_4);
    return 0;
}

例如,简单地省略volatile会导致我的机器上的GCC 4.6.3和MSC 10.0报告花费的0。我正在使用lambda,因为函数指针没有被这些编译器内联,但lambda是。

在我的机器上,GCC上此基准测试的输出是:

Elapsed time for 1073741824 runs of popcount_1: 3.7s
Elapsed time for 1073741824 runs of popcount_2: 3.822s
Elapsed time for 1073741824 runs of popcount_3: 4.091s
Elapsed time for 1073741824 runs of popcount_4: 23.821s

和MSC:

Elapsed time for 1073741824 runs of popcount_1: 7.508s
Elapsed time for 1073741824 runs of popcount_2: 5.864s
Elapsed time for 1073741824 runs of popcount_3: 3.705s
Elapsed time for 1073741824 runs of popcount_4: 19.353s