c++运行一个程序时如何打印出执行程序的时间

https://blog.csdn.net/weibo1230123/article/details/81383073

#include<iostream>
#include<time.h>//ctime
using namespace std;
void insert_sort(int a[], int len)
{
	for (int i = 1; i < len; i++)
	{
		int temp = a[i];
		int j = i;
		while (j > 0 && a[j - 1] > temp)
		{
			a[j] = a[j - 1];
			j--;
		}
		a[j] = temp;
	}
}

int main()
{
	int arr[100];
	int s;
	int startime, endtime;
	cin >> s;
	for (int i = 0; i < s; i++)
		cin >> arr[i];
	int n = sizeof(arr) / sizeof(arr[0]);
	insert_sort(arr, s);
	startime = clock();
	for (int i = 0; i < s; i++)
		cout<<arr[i]<<" ";
	endtime = clock();
	cout << "T=" << (double)(endtime-startime) / CLOCKS_PER_SEC;
	return 0;
}

发布了38 篇原创文章 · 获赞 2 · 访问量 1199

猜你喜欢

转载自blog.csdn.net/weixin_44811068/article/details/103015671
今日推荐