9个数中取最大值最小值速度问题

项目中需要大量计算9个数中取最小值,现在测试一下几种方法的速度。

软件环境VC 6.0 + Windows xp sp3;

硬件环境 Pentium G630 双核 2.7GHz,3GB内存。

第一种方法:#define min(a,b) a<b?a:b

#include <iostream.h>
#include <TIME.H>
#define min(a,b) a<b?a:b
#include <stdlib.h>

int main()
{
    clock_t start, finish;
    long duration;
    int minimum_in9 = 0;
    int array[9];
    srand((unsigned)time(0));
    for(int i=0;i<9;i++)
    { 
        array[i]=rand() % 100;
        cout<<array[i]<<" ";
    }
    cout<<"\n";
    start = clock();
    for (int t=0;t<100000000;t++)
    {
        int a1 = min(array[0],array[1]);
        int a2 = min(array[2],array[3]);
        int a3 = min(array[4],array[5]);
        int a4 = min(array[6],array[7]);
        int b1 = min(a1,a2);
        int b2 = min(a3,a4);
        int c1 = min(b1,b2);
        minimum_in9 = min(c1,array[8]);
    }
    finish = clock();
    cout<<"minimum_in9 = "<<minimum_in9<<"\n";
    duration = (long)(finish-start);
    cout<<"total time is = "<<duration<<" milliseconds"<<endl;
    return 1;
}

 

这种方法在1亿次循环中,耗时大约1100毫秒(1068~1170)。

 第二种方法:使用二维矩形循环比较

View Code
#include <iostream.h>
#include <TIME.H>
#include <stdlib.h>

int main()
{
    clock_t start, finish;
    long duration;
    int minimum_in9;
    int array[9];
    srand((unsigned)time(0));
    for(int i=0;i<9;i++)
    { 
        array[i]=rand() % 100;
        cout<<array[i]<<" ";
    }
    cout<<"\n";
    start = clock();
    for (int t=0;t<100000000;t++)
    {
        minimum_in9 = 0;
        for (int i=0;i<3;i++)
        {
            for (int j=0;j<3;j++)
            {
                int temp = array[i*3+j];
                minimum_in9 = (minimum_in9<temp)?temp:minimum_in9;
            }
        }
    }
    finish = clock();
    cout<<"minimum_in9 = "<<minimum_in9<<"\n";
    duration = (long)(finish-start);
    cout<<"total time is = "<<duration<<" milliseconds"<<endl;
    return 1;
}

这种方法在1亿次循环中,耗时大约3650毫秒(3578~3687)。

第三种方法:使用一维矩形循环比较

View Code
#include <iostream.h>
#include <TIME.H>
#include <stdlib.h>

int main()
{
    clock_t start, finish;
    long duration;
    int minimum_in9;
    int array[9];
    srand((unsigned)time(0));
    for(int i=0;i<9;i++)
    { 
        array[i]=rand() % 100;
        cout<<array[i]<<" ";
    }
    cout<<"\n";
    start = clock();
    for (int t=0;t<100000000;t++)
    {
        minimum_in9 = 0;
        for (int i=0;i<9;i++)
        {
            minimum_in9 = (minimum_in9<array[i])?array[i]:minimum_in9;
        }
    }
    finish = clock();
    cout<<"minimum_in9 = "<<minimum_in9<<"\n";
    duration = (long)(finish-start);
    cout<<"total time is = "<<duration<<" milliseconds"<<endl;
    return 1;
}

 这种方法在1亿次循环中,耗时大约2800毫秒(2734~2859)。

第四种方法:将第一种方法改为调用函数

View Code
#include <iostream.h>
#include <TIME.H>
#include <stdlib.h>
min(int a,int b);
int main()
{
    clock_t start, finish;
    long duration;
    int minimum_in9 = 0;
    int array[9];
    srand((unsigned)time(0));
    for(int i=0;i<9;i++)
    { 
        array[i]=rand() % 100;
        cout<<array[i]<<" ";
    }
    cout<<"\n";
    start = clock();
    for (int t=0;t<100000000;t++)
    {
        int a1 = min(array[0],array[1]);
        int a2 = min(array[2],array[3]);
        int a3 = min(array[4],array[5]);
        int a4 = min(array[6],array[7]);
        int b1 = min(a1,a2);
        int b2 = min(a3,a4);
        int c1 = min(b1,b2);
        minimum_in9 = min(c1,array[8]);
    }
    finish = clock();
    cout<<"minimum_in9 = "<<minimum_in9<<"\n";
    duration = (long)(finish-start);
    cout<<"total time is = "<<duration<<" milliseconds"<<endl;
    return 1;
}

int min(int a,int b)
{
    return a<b?a:b;
}

这种方法在1亿次循环中,耗时大约13800毫秒(13718~13937)。

第五种方法:调用函数比较

View Code
#include <iostream.h>
#include <TIME.H>
#include <stdlib.h>
int min(int a, int b, int c);
int main()
{
    clock_t start, finish;
    long duration;
    int minimum_in9 = 0;
    int array[9];
    srand((unsigned)time(0));
    for(int i=0;i<9;i++)
    { 
        array[i]=rand() % 100;
        cout<<array[i]<<" ";
    }
    cout<<"\n";
    start = clock();
    for (int t=0;t<100000000;t++)
    {
        int b1 = min(array[0],array[1],array[2]);
        int b2 = min(array[3],array[4],array[5]);
        int b3 = min(array[6],array[7],array[8]);
        minimum_in9 = min(b1, b2, b3);
    }
    finish = clock();
    cout<<"minimum_in9 = "<<minimum_in9<<"\n";
    duration = (long)(finish-start);
    cout<<"total time is = "<<duration<<" milliseconds"<<endl;
    return 1;
}

int min(int a, int b, int c)
{
    if(a>b) a=b;
    if(a>c) a=c;
    return a;
}

这种方法在1亿次循环中,耗时大约7000毫秒(6906~7031)。

posted @ 2013-02-28 12:26  水上云天  阅读(325)  评论(0编辑  收藏  举报