fun2比fun1稍快点

简介: fun2比fun1稍快点

fun2比fun1稍快点

运行环境:WinXP VS2005(VC8)

核心代码:

#include <iostream>
using namespace std ;
#include <time.h>
#define WAN (10000)
const int iNum = WAN*WAN*3;
void fun1(int* p)
{
    for( int i = 0 ; i < iNum ; i++ )
    {
        p[i] = i;
    }
}
void fun2(int* p)
{
    for( int i = 0 ; i < iNum ; i+= 4  )
    {
        p[i] = i;
        p[i+1] = i+1;
        p[i+2] = i+2;
        p[3+3] = i+3 ;
    }
}


测试程序一:

void main()
{
    int * p = new int[iNum];
    int x1 = time(NULL);
    fun1(p);
    int x2 = time(NULL);
    fun2(p);
    int x3 = time(NULL);
    cout << (long)(x2-x1) << "秒 " << (long)(x3-x2) << "秒";
    delete [] p ;
}

五次的运行结果:

4,3

5,2

4,3

5,2

4,3


测试程序二fun2在前,fun1在后:

void main()
{
    int * p = new int[iNum];
    int x1 = time(NULL);
    fun2(p);
    int x2 = time(NULL);
    fun1(p);
    int x3 = time(NULL);
    cout << (long)(x2-x1) << "秒 " << (long)(x3-x2) << "秒";
    delete [] p ;
}

五次的运行结果:

3,4

3,5

3,4

2,5

3,4


总结3亿次循环,可以节约1秒左右。



p[3+3] = i+3 ; 弄错了,改正。 时间精确到毫秒。


#include <iostream>
using namespace std ;
#include <time.h>
#define WAN (10000)
const int iNum = WAN*WAN*3;
void fun1(int* p)
{
    for( int i = 0 ; i < iNum ; i++ )
    {
        p[i] = i;
    }
}
void fun2(int* p)
{
    for( int i = 0 ; i < iNum ; i+= 4  )
    {
        p[i] = i;
        p[i+1] = i+1;
        p[i+2] = i+2;
        p[i+3] = i+3 ;
    }
}
void Test()
{
    int * p = new int[iNum];
    long x1 = clock();
    fun1(p);
    long x2 = clock();
    fun2(p);
    long x3 = clock();
    cout << (x2-x1) << "豪秒 " << (x3-x2) << "豪秒" << endl;
    delete [] p ;
}
void main()
{
   Test();
   Test();
   Test();
   Test();
   Test();
}

结果为:


4734豪秒 3547豪秒

5078豪秒 3047豪秒

4453豪秒 3047豪秒

4468豪秒 2954豪秒

4531豪秒 3063豪秒

换一下Test中fun1,fun2的位置:

void Test()
{
    int * p = new int[iNum];
    long x1 = clock();
    fun2(p);
    long x2 = clock();
    fun1(p);
    long x3 = clock();
    cout << (x2-x1) << "豪秒 " << (x3-x2) << "豪秒" << endl;
    delete [] p ;
}


结果为:


3016豪秒 4313豪秒

2938豪秒 4344豪秒

2953豪秒 4515豪秒

2875豪秒 4343豪秒

2875豪秒 4329豪秒

结论:也是快1.5秒。


相关文章
|
5月前
|
C语言
c语言编程练习题:7-49 Have Fun with Numbers
c语言编程练习题:7-49 Have Fun with Numbers
55 0
|
3月前
|
语音技术 Python
语音识别,continue和break的使用,循环综合案例,完成发工资案例,函数的初体验,len()是内置好的函数,def 函数名 def xxx(),函数的定义 def xxx() ,调用函数
语音识别,continue和break的使用,循环综合案例,完成发工资案例,函数的初体验,len()是内置好的函数,def 函数名 def xxx(),函数的定义 def xxx() ,调用函数
|
5月前
|
编译器 程序员 Serverless
函数...(一)
该内容介绍了C语言中的函数概念,包括库函数和自定义函数。库函数是预先定义并实现的函数,如`printf`和`scanf`,它们在不同的头文件中声明,使用时需要包含相应的头文件。自定义函数则是用户根据需求编写的函数,具有自己的函数名、参数和返回类型。函数通过形参和实参交互,实参是调用时传递的实际值,形参是函数定义中的占位符。
38 2
|
5月前
VS2015中main函数带参数运行
VS2015中main函数带参数运行
100 0
|
5月前
|
编译器 C语言
函数...(二)
函数的`return`语句用于结束函数执行并返回结果。它可以带一个表达式或不带,带表达式时会先计算表达式再返回其值,不带时适用于`void`类型的函数。如果返回值与函数声明的返回类型不符,系统会自动转换。确保所有分支都有`return`,避免编译错误。函数是否需要返回值取决于实际需求。数组可作为参数传递,但函数调用不能嵌套定义,却能链式调用,即一个函数的返回值作为另一个函数的参数。函数需先声明后使用,尤其在多文件项目中,声明通常在头文件,实现则在源文件中。
21 0
|
5月前
|
存储 Shell Python
学Python,还不知道main函数吗
学Python,还不知道main函数吗
假定循环内用时较多,fun1比fun2快多少
假定循环内用时较多,fun1比fun2快多少
初学算法之---pta fun with numbers
初学算法之---pta fun with numbers
C++(main函数知识点)
C++(main函数知识点)