运行环境: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秒左右。
时间: 2024-10-08 05:23:08