Demo entry 6352249

lal

   

Submitted by anonymous on Mar 24, 2017 at 09:17
Language: C++. Code size: 1.1 kB.

#include<iostream>
#include<math.h>
#include<algorithm>
#include<unistd.h>
#include<sys/time.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

int size;//the size of matrix

int a[16][16] = {0};

int temp;

int main()
{
	size = 16;
		
	for(int i = 0; i < size; i++)//使用随机数生成10000*10000的矩阵
	{
		for(int j = 0; j < size; j++)
		{
			a[i][j] = rand()%10;
		}
	}
	
	for(int i = 0; i < size; i++)
	{
		for(int j = 0; j < size; j++)
		{
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	}

	struct timeval tv;
	gettimeofday(&tv, NULL);
	double time_start, time_end;
	time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;//获取开始时间

	
	for(int j = 0; j < size; j++)
	{
		for(int k = 0; k < j; k++)
		{
			temp = a[j][k];
			a[j][k] = a[k][j];
			a[k][j] = temp;
		}
	}

	gettimeofday(&tv, NULL);
	time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;//获取结束时间

	for(int i = 0; i < size; i++)
	{
		for(int j = 0; j < size; j++)
		{
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	}
	
	cout<<"  the cost time is:"<<time_end-time_start<<endl;//计算转置所花总时间
	
	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).