Dart 运行速度测评与比较
SKYGXK 人气:0引言
Dart 是一门优秀的跨平台语言,尽管生态方面略有欠缺,但无疑作为一门编程语言来说,Dart 是很优美,很健壮的,同时也引入了一些先进的编程范式,值得去学习。
测试内容
现在,我们就来测评一下Dart 语言的运行速率,测试平台为WSL(win子系统,Ubuntu18.04),有以下几个对比项:
编程语言 | 版本 |
---|---|
Dart VM | Dart VM 2.7.1 |
Dart-to-JavaScript | dart2js 2.7.1 |
Dart-to-Native | 同上 |
Java | openjdk 11.0.6 |
C# | .NET Core SDK 3.1.102 |
C | gcc 7.5.0 |
JavaScript | node 8.10.0 |
Python | Python 3.6.9 |
测试用代码如下:
for (i = 0; i < 10000; i++)
for (j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
最后统计此代码段运行时间,各个语言的具体代码见本文最后。
测试结果
编程语言 | 运行指令 | 运行时间/ms |
---|---|---|
Dart VM | dart t.dart | 375 |
Dart-to-JavaScript | dart2js t.dart && node out.js | 638 |
Dart-to-Native | dart2native t.dart && ./t.exe | 631 |
Java | javac t.java && java t | 126 |
C# | dotnet restore && dotnet run | 964 |
C | gcc t.c && ./a.out | 313 |
JavaScript | node t.js | 245 |
Python | python3 t.py | 22403 |
额...与我设想的结果好像不太一样...那我们来排个序:
- Java
- JavaScript
- C
- Dart VM
- Dart-to-Native
- Dart-to-JavaScript
- C#
- Python
首先声明一下,就语言本身的优美程度来讲,我的确是有点不喜欢Python,但我也没想到它会这么慢...还不是一个数量级,开始还以为电脑卡住了,又试了几下大概都还是这么多。当然,Python的强项也不在此,而且如果使用numpy
优化处理该代码段的话,速度一定不会这么不堪。
同时C#的.net core也被openjdk远远甩开了,果然,C#最好还是要用在windows上。本来我是要更喜欢C#的,哎但是这个速度...
最让我惊讶的是,本来感觉毫无疑问应该排第一的C语言竟然被两种解释性语言给甩开了。也...许,是WSL的问题?
总之,Dart的运行速率基本在正常范围内,并且运行也很方便,再就是跨平台性,flutter也将赋予dart更强大的生命力。
测试代码
Java
public class t {
public static long[][] a = new long[10000][];
public static void main(String[] args) {
long t0, t1;
for (int i = 0; i < 10000; i++)
a[i] = new long[10000];
t0 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
t1 = System.currentTimeMillis();
System.out.println("Running Time: " + (float) (t1 - t0) + "ms");
}
}
JavaScript
a = new Array(10000);
for (let i = 0; i < 10000; i++)
a[i] = new Array(10000);
console.time('Running Time');
for (let i = 0; i < 10000; i++)
for (let j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
console.timeEnd('Running Time');
C
#include <stdio.h>
#include <time.h>
int a[10000][10000];
int main()
{
int i, j;
double t1, t2;
t1 = clock();
for (i = 0; i < 10000; i++)
for (j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
t2 = clock();
printf("Running Time: %fms\n", 1000 * (double)(t2 - t1) / CLOCKS_PER_SEC);
return 0;
}
Dart
void main() {
var a = List(10000);
for (int i = 0; i < 10000; i++)
a[i] = List(10000);
var timer = Stopwatch();
timer.start();
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
timer.stop();
print('Running Time: ' + timer.elapsedMilliseconds.toString() + 'ms');
}
C
using System;
namespace t
{
class Program
{
static int[,] a = new int[10000,10000];
static void Main(string[] args)
{
DateTime beforDT = System.DateTime.Now;
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
a[i,j] = i * j + 1;
DateTime afterDT = System.DateTime.Now;
TimeSpan ts = afterDT.Subtract(beforDT);
Console.WriteLine("Running Time: {0}ms", ts.TotalMilliseconds);
}
}
}
Python
from time import time
a = [[0 for i in range(10000)] for j in range(10000)]
t0 = time()
for i in range(1000):
for j in range(10000):
a[i][j] = i * j + 1
t1 = time()
print ('Running Time: ' + str(1000*(t1-t0)) + 'ms')
原文链接 https://www.cnblogs.com/skygxk/p/12404054.html
加载全部内容