0

我很感兴趣如何把它放在循环中,以便获得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 操作对于现代计算机来说不是什么,但我怎么能点这样才能得到如果不是准确的答案,近似值至少?非常感谢

4

2 回答 2

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):)

于 2011-11-14T11:33:18.630 回答
3

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

  • 围绕廉价操作进行循环;一个需要足够长的时间来合理安排时间;例如大约一秒钟。
  • 确保在下一个循环迭代中使用结果,以避免编译器完全省略主体。
  • 将整个循环包装在一个函数中,使用特定于编译器的属性将该函数标记为不可内联(再次确保编译器不会忽略调用)并从您的计时函数中调用函数。 或者,根据所有循环迭代返回一个值,并在主程序中实际使用该返回值(例如打印它或将其存储在变量中),以确保编译器不能只是优化程序并将其删除。volatile
  • 此外,您应该使用高分辨率计时器而不是clock(). 在 windows 上这将是QueryPerformanceCounter(&tick_count),在 unix 上clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &timespec_var),在 macos 上看看mach_absolute_time()。(其中一些)这些方法的另一个优点是它们测量 CPU 时间,而不是挂钟时间,因此在面对系统上的其他活动时变化略小。

同样,确保您实际使用通过将它们存储在变量中、打印它们或从非内联函数返回它们以确保编译器不能仅仅优化它们来实际使用计算的值是绝对关键的。而且您不想将核心方法标记为不可内联,因为函数调用开销可能会淹没此类微基准测试;出于类似的原因,您可能应该避免使用. 这就是为什么您应该对包含调用您实际感兴趣的(可内联)函数的循环的函数进行基准测试。volatilerandom

例如:

#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
于 2011-11-14T11:42:52.050 回答