Compute C + + program execution time to the millisecond level __c++

Source: Internet
Author: User
Tags rand

Refer to others ' own summary. I have a limited level and have trouble pointing out mistakes.

Two methods: (Please ignore bubble sort)

First type:

#include <iostream> #include <vector> #include <time.h> #include <sys/timeb.h> #include <
Windows.h> using namespace std;
	void Bubble_sort1 (vector<int> &t) {int length = t.size ();
			for (int i = 0; i < length-1. i++) {for (int j = 0; J < length-1-I. j) {if (T[j] > t[j + 1])
				{int temp = t[j];
				T[J] = t[j + 1];
			T[j + 1] = temp;
and/or upgraded version, added flag to determine if a cycle has been exchanged without directly ending.
	void Bubble_sort2 (vector<int> &t) {int length = t.size ();
		for (int i = 0; i < length-1 i++) {BOOL flag = false;
				for (int j = 0; J < length-1-I; j + +) {if (T[j] > t[j + 1]) {int temp = t[j];
				T[J] = t[j + 1];
				T[j + 1] = temp;
			Flag = true;
		} if (!flag) {break;
	int main () {DWORD start1, end1;
	Start1 = GetTickCount ();
	Srand ((unsigned) time (0));
	Vector<int> v;
		for (int i = 0; i < 1700 i++) {V.push_back (rand ()); V.push_back (rand ()% 10);//Generate 0 to 9 random number} bubble_sort1 (v);
	End1 = GetTickCount ();
	cout << "Cost Time (ms):" << end1-start1 << Endl;
return 0; }

The second type:

#include <iostream> #include <vector> #include <time.h> #include <sys/timeb.h> #include <
Windows.h> using namespace std;
	void Bubble_sort1 (vector<int> &t) {int length = t.size ();
			for (int i = 0; i < length-1. i++) {for (int j = 0; J < length-1-I. j) {if (T[j] > t[j + 1])
				{int temp = t[j];
				T[J] = t[j + 1];
			T[j + 1] = temp;
and/or upgraded version, added flag to determine if a cycle has been exchanged without directly ending.
	void Bubble_sort2 (vector<int> &t) {int length = t.size ();
		for (int i = 0; i < length-1 i++) {BOOL flag = false;
				for (int j = 0; J < length-1-I; j + +) {if (T[j] > t[j + 1]) {int temp = t[j];
				T[J] = t[j + 1];
				T[j + 1] = temp;
			Flag = true;
		} if (!flag) {break;
	int main () {struct TIMEB start, end;
	Ftime (&start);
	Srand ((unsigned) time (0));
	Vector<int> v;
		for (int i = 0; i < 1700 i++) {V.push_back (rand ()); V.push_back (rand ()% 10)//Generate 0 to 9 random number} bubble_sort1 (v);
	Ftime (&end);
	cout << "Cost Time (ms):" << (end.time-start.time) * 1000 + (END.MILLITM-START.MILLITM) << Endl;
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.