matlab Java C python 计算速度 谁更快?

目录

  • 结论
  • 实验
    • matlab
    • Java
    • C语言
    • python
  • 知识点

处理器:11th Gen Intel® Core™ i7-11370H @ 3.30GHz 3.30 GHz
(看懂cpu参数 https://www.bilibili.com/video/BV1854y1j7xL)

16G 内存,Windows10 64位操作系统

Matlab R2021b
IntelliJ IDEA 2021.1.3
Devc++ 5.11
python3.9 32位,Pycharm2021.1.2

结论

matlab >> Java > c > python

matlab Java C python 计算速度 谁更快?_第1张图片

matlab的问题规模 = 10倍Java问题规模
Java问题规模 = c问题规模
c问题规模 = 10倍python问题规模

因为直接插入排序的时间复杂度(也可以看成是计算复杂度)是n2
故:
matlab的计算量 = 100倍Java计算量
Java计算量 = c计算量
c计算量 = 100倍python计算量

实验

用 for循环 实现直接插入排序
时间复杂度 n2

matlab

l = 2000000;
b = linspace(l,15000000,14);
a = zeros(1,1500000);

fprintf('开始直接插入排序(for) \n\n');

for l = b
    tic % 开始计时
    length = l;
    fprintf('length = %s\n', num2str(length));

    for k = 1:length % 步长+1 for循环
        a(k) = unidrnd(length); % 随机整数
    end

    % 开始排序
    for i=2:length 
        temp = a(i);
        for j = (i-1):(length)
		    if temp<a(j) 
                a(j + 1) = a(j);
                continue;
            else
                break;
            end
        end
		a(j + 1) = temp;
    end

    toc % 计时完成
%    fprintf('排序完成!  用时 = %s\n\n', num2str(toc)) % num2str 数字转为字符
    fprintf('\n') % num2str 数字转为字符
end

Java

public class paixu {
    public static void main(String[] args) {

        long start ,end;
        Random rand = new Random();
        String sn;
        long[] a = new long[1500000];

        double Total_time, s;
        long length;

        long l1 = 200000;
        long[] b = new long[14];

        for(int i = 0;i<14;i++)
        {
            b[i] = l1;
            l1 = l1+100000;
        }


        for(int l=0;l<14;l++)
        {
            s = 0;
            length = b[l];
            System.out.println("length = " + length);
            System.out.print("开始直接插入排序(for)  ");
            System.out.print("排序完成! ");
            System.out.print("用时 = ");


            start = System.currentTimeMillis();   //获取开始时间

            for(int k = 0; k<length; k++)
            {
                a[k] = rand.nextInt(10000);
            }

            int i, j;
            long temp;

            for(i = 1; i < length; i++)
            {
                temp = a[i];
                for(j = i-1; j >= 0; j--)
                {
                    if(temp < a[j])
                        a[j + 1] = a[j];
                    else
                        break;
                }
                a[j + 1] = temp;
            }

            end = System.currentTimeMillis();

            s = (end-start)/1000.0;
            sn = String.format("%4f", s);
            System.out.println("程序运行时间: "+sn+"s \n");
        }
    }
}

C语言

#include
#define num 10000
#define cmp <
using namespace std;

long a[1500000];

int main()
{
	srand(time(NULL));
	clock_t start, end; 
	double Total_time, s;
	long length; 
	
	long l = 200000;
	long b[14];
	for(int i = 0;i<14;i++) 
	{
		b[i] = l;
		l = l+100000;
	}
	

	for(l=0;l<14;l++)
	{ 
		s = 0;
		length = b[l];
		cout << "length = " << length <<  endl;
		cout << "开始直接插入排序(for)  ";
		cout << "排序完成! "<< endl;
		cout << "用时 = " ;
		
		for(int q = 0;q<10;q++)
		{
			start = clock();
			int k;
			
			for(k = 0;k<length;k++)
			{
				a[k] = rand()%num;
			}
			
		
			int i, j, temp;
			
			for(i = 1; i < length; i++)
			{
				temp = a[i];
				for(j = i-1; j >= 0; j--)
				{
					if(temp cmp a[j])
						a[j + 1] = a[j];
					else
						break;
				}
				a[j + 1] = temp;
			}
			
			
			end = clock();
			Total_time = (double)(end - start) / CLOCKS_PER_SEC;
			cout << Total_time << "  ";
			s = s + Total_time;
			start = end;
		}
		s = s/10;
		cout << endl << s << endl << endl;
		
	}
	
	system("pause"); 
	return 0;
}

python

import random
import time

a=[1]*200000;

b=[1]*14;
l = 20000;
for i in range(0,14):
    b[i] = l;
    l = l + 10000;

print("开始直接插入排序 \n");

for l in range(0,14):
    start = time.time()
    s = 0;
    length = b[l];

    print("length =",length);

    for k in range(0,length):
        a[k] = random.randint(0,10000);

    for i in range(1,length):
        temp = a[i];
        for j in range(i-1, -1, -1):
            if temp < a[j]:
                a[j + 1] = a[j];
            else:
                break;
        a[j+1] = temp;

    end = time.time();
    print('用时 = ', '%.3f'%(end - start), 's\n')

matlab Java C python 计算速度 谁更快?_第2张图片

matlab的计算量 = 10倍Java计算量
Java计算量 = c计算量
c计算量 = 10倍python计算量

故:matlab >> Java > c > python(计算速度)

处理器:11th Gen Intel® Core™ i7-11370H @ 3.30GHz 3.30 GHz
(看懂cpu参数 https://www.bilibili.com/video/BV1854y1j7xL)

16G 内存,Windows10 64位操作系统

Matlab R2021b
IntelliJ IDEA 2021.1.3
Devc++ 5.11
python3.9 32位,Pycharm2021.1.2

知识点

matlab Java C python 计算速度 谁更快?_第3张图片

你可能感兴趣的:(记录,matlab,java,c语言)