64,666
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int a[100000];
long start=clock(),end(0);
for(int i=0;i<100000;i++)
a[i]=i;
for(int i=0;i<100000;i++)
sum+=a[i];
end=clock();
long result=(end-start)/1000;
cout<<"The time was: "<<result<<endl;
system("pause");
return 0;
}
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
int a[100000];
int sum = 0;
LARGE_INTEGER fr;
QueryPerformanceFrequency(&fr);
LARGE_INTEGER t1, t2;
QueryPerformanceCounter(&t1);
for(int i=0;i<100000;i++)
a[i]=i;
for(int i=0;i<100000;i++)
sum+=a[i];
QueryPerformanceCounter(&t2);
long result = static_cast<long>((t2.QuadPart - t1.QuadPart) * 1000000 / fr.QuadPart);
cout<<"The time was: "<<result<<"us"<<endl;
system("pause");
return 0;
}
// crt_clock.c
// This example prompts for how long
// the program is to run and then continuously
// displays the elapsed time for that period.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sleep( clock_t wait );
int main( void )
{
long i = 6000000L;
clock_t start, finish;
double duration;
// Delay for a specified time.
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );
// Measure the duration of an event.
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}
// Pauses for a specified number of milliseconds.
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}